I'm having trouble getting the Web API to work with subdomain-based routes. In short, I get to the correct controller and method, but the data token from the subdomain is not matched by WebAPI.
I have this in my scenario:
contoso.myapp.com fabrikam.myapp.com {tenant}.myapp.com
All permissions on the same ApiController, and I want to be able to extract the {tenant} token.
I used the code in this article http://blog.maartenballiauw.be/post/2012/06/18/Domain-based-routing-with-ASPNET-Web-API.aspx
But there is something that seems to have changed between the time this article was written and the beta release of ASP.NET Web Api. The code in the article relies on RouteTable.Routes , while the web API routes are configured through HttpConfiguration.Routes , which is an HttpRouteCollection , not a regular RouteCollection (it actually comes from a RouteCollection ).
So, I changed the code to get HttpRoute instead of Route . Here is the code:
https://gist.github.com/3766125
I configure the route as follows
config.Routes.Add(new HttpDomainRoute( name: "test", domain: "{tenant}.myapp.com", routeTemplate: "test", defaults: new { controller = "SomeController", action = "Test" } ));
And my requests go to the right controller. However, the tenant's data token is never populated (if I do this.Request.GetRouteData() , I see controller tokens and actions, but not tenants). If I set a breakpoint on GetRouteData , it is never called.
I tried to execute the code path with a reflector and see where GetRouteData is called at the HttpRouteCollection level, but it seems that the collection is an enumeration empty. Not sure exactly how regular ASP.NET routing and WEB API routing connect, but that confuses me.
Any ideas?
The workaround I'm using now is calling GetRouteData explicitly on Route
this.Request.GetRouteData().Route.GetRouteData(this.Request.RequestUri.ToString(), this.Request)