locked
MapHttpRoute, same named controllers, different namespaces RRS feed

  • Question

  • User-1447435113 posted

    Hi,

    I think here is the right place for asking my question. This is my case:

    //--------------------
    //ClientController.cs
    //--------------------
    
    namespace Client
    {
        public class DetailController : ApiController
        {
            .
            .
            .
        }
    }
    
    //--------------------
    //OrderController.cs
    //--------------------
    
    namespace Order
    {
        public class DetailController : ApiController
        {
            .
            .
            .
        }
    }
    
    //------------
    // Global.asax
    //------------
    
    .
    .
    .
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapHttpRoute
        (
            name: "ClientApi",
            routeTemplate: "client/{controller}/{param1}"
        );
    
        routes.MapHttpRoute
        (
            name: "OrderApi",
            routeTemplate: "order/{controller}/{param1}"
        );
    }
    .
    .
    .

    What I'm trying to do?
    I want to make api calls like this:

    http(s)://myapi/client/detail/PARAM1 --> supposed to "show" detail of CLIENT named PARAM1
    http(s)://myapi/order/detail/PARAM1 --> supposed to "show" detail of ORDER named PARAM1

    As you see, there are two ApiControllers, "DetailController", have the same name but in different Namespaces. Compiler doesn't throw an exception but when I make a call,

    "Multiple types were found that match the controller named 'detail'. This can happen if the route that services this request ('client/{controller}/{param1}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. The request for 'detail' has found the following matching controllers: ...client.DetailController ...order.DetailController"

    message comes. It suggests me to use MapRoute instead of MapHttpRoute.

    How can I achieve this?

    Saturday, February 25, 2012 7:00 AM

Answers

  • User-1447435113 posted

    I found this topic, answered by marcind. According to marcind, one of the member of Web Api developer team, "namespace" feature doesn't exist currently. I hope that "namespace" feature is going to be included in this final release of Web Api.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 25, 2012 3:25 PM

All replies

  • User1220103879 posted

    Controllers with the same name in different namespaces aren't supported in this version. One thing you can do to support the URL space you want is to do something like the following:

    namespace Client {
        public class ClientDetailController : ApiController
        {
            ...
        }
    }
    
    
    namespace Order {
        public class OrderDetailController : ApiController
        {
            ...
        }
    }
    
    // global.asax
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapHttpRoute(
            name: "ClientApi",
            routeTemplate: "client/detail/{param1}",
            defaults: new { controller = "ClientDetail" }
        );
    
        routes.MapHttpRoute(
            name: "OrderApi",
            routeTemplate: "order/detail/{param1}",
            defaults: new { controller = "OrderDetail" }
        );
    }
    Saturday, February 25, 2012 10:27 AM
  • User-1447435113 posted

    Thank you CarlosFigueira for your response but I tried that before and I received:

    "No type was found that matches the controller named 'detail'." message.

    I think, MapHttpRoute ignores controller parameter in defaults:

    Saturday, February 25, 2012 10:50 AM
  • User-1447435113 posted

    Thank you BrockAllen for your response but I also tried that before and I got same result.

    Saturday, February 25, 2012 10:54 AM
  • User-1447435113 posted

    Sorry CarlosFigueira. I didn't see your change in routeTemplate (Changing /{controller}/ to /detail/ which makes the controller is fixed).

    I also tried it before. It worked but my question is a simplified version of whole story. This solution is seen useless to me. Because, if I want to add another controller(s) to "ClientController", I have to fix controllers' names in this way. This means, lots of routes have to be defined in Global.asax file. I feel that it will be very hard to handle those routes in future.

    Saturday, February 25, 2012 11:13 AM
  • User-1447435113 posted

    I found this topic, answered by marcind. According to marcind, one of the member of Web Api developer team, "namespace" feature doesn't exist currently. I hope that "namespace" feature is going to be included in this final release of Web Api.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 25, 2012 3:25 PM