Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Web API RTM and subdomain routes
    primarykey
    data
    text
    <p>I am having issues getting Web API to work with subdomain-based routes. In short, I am getting to the right controller and method but the data token out of the subdomain is not being picked up by WebAPI.</p> <p>I have this in my scenario:</p> <pre><code>contoso.myapp.com fabrikam.myapp.com {tenant}.myapp.com </code></pre> <p>All resolving to the same ApiController and I want to be able to extract the <code>{tenant}</code> token.</p> <p>I used the code in this article <a href="http://blog.maartenballiauw.be/post/2012/06/18/Domain-based-routing-with-ASPNET-Web-API.aspx" rel="nofollow">http://blog.maartenballiauw.be/post/2012/06/18/Domain-based-routing-with-ASPNET-Web-API.aspx</a></p> <p>But there is something that seem to have changed between the time the article was written and ASP.NET Web Api went out of beta. The code in the article is relying on <code>RouteTable.Routes</code> while on Web API routes are configured through <code>HttpConfiguration.Routes</code> which is an <code>HttpRouteCollection</code> and not the usual <code>RouteCollection</code> (it derives from <code>RouteCollection</code> actually).</p> <p>So I changed the code to derive from <code>HttpRoute</code> instead of <code>Route</code>. Here is the code:</p> <p><a href="https://gist.github.com/3766125" rel="nofollow">https://gist.github.com/3766125</a> </p> <p>I configure a route like this</p> <pre><code> config.Routes.Add(new HttpDomainRoute( name: "test", domain: "{tenant}.myapp.com", routeTemplate: "test", defaults: new { controller = "SomeController", action = "Test" } )); </code></pre> <p>And my requests are routed to the right controller. However, the tenant data token is never filled (if I do <code>this.Request.GetRouteData()</code> I see the controller and action tokens but not tenant). If I put a breakpoint on <code>GetRouteData</code> it is never called.</p> <p>I tried to follow the code path with reflector and see where GetRouteData is being called at the HttpRouteCollection level, but it seems that the collection is enumarating is empty. Not sure exactly how the integration between regular ASP.NET routing and WEb API routing is bridged but it is confusing me.</p> <p><strong>Any ideas?</strong></p> <p>The workaround I am using for now is calling explicitly GetRouteData over the <code>Route</code></p> <p><code>this.Request.GetRouteData().Route.GetRouteData(this.Request.RequestUri.ToString(), this.Request)</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload