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

烂漫一生 提交于 2019-12-04 08:20:46

问题


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


回答1:


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




回答2:


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/




回答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




回答4:


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.



来源:https://stackoverflow.com/questions/2047146/can-areas-in-an-asp-net-mvc-2-application-map-to-a-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!