0

This is ok:

    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional });

But I want to redirect incoming requests to ApiFolder folder's class enter image description here

Kara
  • 6,115
  • 16
  • 50
  • 57
uzay95
  • 16,052
  • 31
  • 116
  • 182

2 Answers2

0

You should specify your namespace in your route configuration:

var r = GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional },
);
if( r.DataTokens == null ){
  r.DataTokens = new RouteValueDictionary();
}
r.DataTokens["Namespaces"] = new string[] {"ApiFolder"};
Kenneth
  • 28,294
  • 6
  • 61
  • 84
  • r.DateTokens is null, and I can't set ["Namespaces"]. – uzay95 May 17 '13 at 13:12
  • You should should do a `null`-check and instantiate a new `RouteValueDictionary` if it is `null`. I edited my answer. – Kenneth May 17 '13 at 13:34
  • `Property or indexer System.Web.Http.Routing.IHttpRoute.DataTokens cannot be assigned to -- it is read only` DataTokens property is read only ! – uzay95 May 17 '13 at 14:46
  • Hm, you're right, that actually doesn't work. From what I see, this feature is not supported with MapHttpRoute – Kenneth May 17 '13 at 15:05
0

Kenneth's suggestion wont work as stated in the comments.

However you can write your own implementation of IHttpControllerSelector and only assign it when you map the api routes. I used the implementation in this article as a base and modified it.

Then its just the small issue of replacing the default selector after mapping the route in WebApiConfig like this (where CustomControllerSelector is my implementation):

    public static void Register(HttpConfiguration configuration)
    {
        var apiRoute = configuration.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional, controllerNamespace = "api" }
        );

        configuration.Services.Replace(typeof(IHttpControllerSelector), new CustomControllerSelector(configuration));
    }
Norrin
  • 385
  • 2
  • 8