views:

394

answers:

3

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

+6  A: 

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

Eilon
+1 for rather clarifying answer.
Anton
A: 

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/

Rob Volk
+1  A: 

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

TWith2Sugars