it:ad:asp.net:webapi:howto:configure_webapi_routing:defining_namespaces

IT:AD:ASP.NET:WebAPI:HowTo:Configure WebAPI Routing defining Namespaces

Summary

When there are two controllers with the same name, but in different namespaces, MVC can't decide which one to use, unless the route has a namespace contraint of some kind.

There is an overload in MVC that allows this to be defined, but not in WebAPI.

The work around to this omission is as follows:

        r = context.Routes.MapHttpRoute(
            "API.V2_default",
            "api/v2/{controller}/{action}/{id}",
            new { version = AreaName, action = "Index", id = UrlParameter.Optional },
            null
        );

        if (r.DataTokens == null)
        {
            r.DataTokens = new RouteValueDictionary();
        }
        r.DataTokens["Namespaces"] = new string[] { "XAct.Spikes.WebAPI.I03.Areas.API.V2.Controllers" };
  • /home/skysigal/public_html/data/pages/it/ad/asp.net/webapi/howto/configure_webapi_routing/defining_namespaces.txt
  • Last modified: 2023/11/04 02:43
  • by 127.0.0.1