Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've tried a lot of the solutions mentioned on other threads and found things getting too complicated very quickly. It seems like ASP.Net MVC wants you to sub-class Route to do this kind of advanced routing, but it never seemed to work for me. I was never able to get a domain to map to a namespace, so I wound up with "ambiguous controller" exceptions (since I had a home controller in both namespaces).</p> <p>Ultimately I used a constraint to point sub-domains to namespaces.</p> <p>Here's what my route looks like. Notice that this route is for the "api." sub-domain:</p> <pre><code> context.MapRoute( "Api_Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = new SubdomainRouteConstraint("api.") }, new[] { "BendyTree.CloudSpark.Areas.Api.Controllers" } ); </code></pre> <p>Here's the "SubdomainRouteConstraint" class referenced above:</p> <pre><code>public class SubdomainRouteConstraint : IRouteConstraint { private readonly string SubdomainWithDot; public SubdomainRouteConstraint(string subdomainWithDot) { SubdomainWithDot = subdomainWithDot; } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return new Regex("^https?://" + SubdomainWithDot).IsMatch(httpContext.Request.Url.AbsoluteUri); } } </code></pre> <p>It's obviously quite a hack, but I'm really happy with how simple it ended up.</p> <p>You could easily tweek this code to dynamically map a subdomain to an area, but I only have two areas so I just register each area separately. Plus this gives me the freedom to have different routing inside each area.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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