Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 687
mynameiscoffey Avatar asked Jan 12 '10 06:01

mynameiscoffey


People also ask

What is subdomain mapping?

By mapping a subdomain to your blog, both your website and blog will use the same domain and be indexed by search engines using the same domain name. The Domain Mapping set up is similar for mapping a root domain or subdomain.

What is a good place to register routes in an MVC application?

Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.

Can you have two subdomains in a URL?

You create subdomains to help organize and navigate to different sections of your main website. Within your main domain, you can have as many subdomains as necessary to get to all of the different pages of your website.

What is Area folder in MVC?

Area allows us to partition the large application into smaller units where each unit contains a separate MVC folder structure, same as the default MVC folder structure. For example, a large enterprise application may have different modules like admin, finance, HR, marketing, etc.


4 Answers

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:

  • Is it possible to make an ASP.NET MVC route based on a subdomain?
  • Asp.net 3.5 Sp1 Sub-Domain routing?
  • And many others

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

like image 147
Eilon Avatar answered Sep 29 '22 22:09

Eilon


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/

like image 40
Rob Volk Avatar answered Sep 29 '22 22:09

Rob Volk


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

like image 22
TWith2Sugars Avatar answered Sep 29 '22 22:09

TWith2Sugars


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.

like image 22
bendytree Avatar answered Sep 29 '22 22:09

bendytree