Can Areas in an ASP.NET MVC 2 application map to a subdomain?
Asked Answered
N

4

19

Is there a way to map the Areas within an ASP.NET MVC 2 application to subdomains such as

movies.example.com/Theater/View/2

instead of

example.com/Movies/Theater/View/2

where { area = "Movies", controller = "Theater", action = "View", id = 2 }.

Nuclide answered 12/1, 2010 at 6:29 Comment(1)
@mynameiscoffey, have you succeeded in this? I have exactly the same task.Boer
P
11

Areas are not directly related to routing, so your question becomes "does routing support subdomains?"

The answer to that is unfortunately that there is no built-in support for this.

However, the good news is that many people have tried and found success with custom code that builds on top of routing:

If you figure out how to route subdomains without areas then doing it with areas should be no more difficult. Go to your area registration context and use whatever technique you choose inside there. Areas are just a way to group together controllers and views - they don't have very much intrinsic meaning to ASP.NET MVC itself (they do a little bit, but not a lot).

Philana answered 12/1, 2010 at 7:15 Comment(0)
O
5

You can map domains or subdomains to an MVC2 Area easily using the IIS7 URL Rewrite module. Here are two simple rules that map subdomains to two Areas. I added a restriction to not use this rule if the request is for an actual file (i.e. CSS, JS, or image files).

Here is the sample config that goes right in your web.config:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="SubA Area">
                <match url=".*" />
                <action type="Rewrite" url="/SubA/{R:0}" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="suba.mydomain.com" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
            </rule>
            <rule name="SubB Area">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="subb.mydomain.com" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Rewrite" url="/SubB/{R:0}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

You'll have to change the rules below to work with your particular use case. Especially if you have controllers on the root area that you need to use. In that case, just add a condition or create a new rule.

Download the IIS URL Rewrite module (required): http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Obolus answered 29/6, 2010 at 17:33 Comment(0)
D
3

I hit upon the same problem and I've built a solution that worked for me, it's in my blog, hopefully it will be of some use to you.

Cheers Tony

Dias answered 15/7, 2010 at 15:12 Comment(0)
B
1

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).

Ultimately I used a constraint to point sub-domains to namespaces.

Here's what my route looks like. Notice that this route is for the "api." sub-domain:

        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" }
            );

Here's the "SubdomainRouteConstraint" class referenced above:

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);
    }
}

It's obviously quite a hack, but I'm really happy with how simple it ended up.

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.

Baggett answered 24/2, 2011 at 4:53 Comment(1)
Unfortunately doesn't work me. I have 2 Home controllers - one of them is in subdomain. It throws like other solutions 'Multiple types were found that match the controller named 'Home'' exception. When I rename it to unique name works fine. But it is unclear how do you generate links to the subdomain? I've tried @Html.RouteLink("Mobile", "api_default") but it's generates link to the main page of application...Osteoplastic

© 2022 - 2024 — McMap. All rights reserved.