ASP.NET MVC 2 应用程序中的区域可以映射到子域吗?

发布于 2024-08-17 21:41:59 字数 220 浏览 16 评论 0原文

有没有办法将 ASP.NET MVC 2 应用程序中的区域映射到子域,例如

movies.example.com/Theater/View/2

而不是

example.com/Movies/Theater /View/2

其中{area =“电影”,控制器=“剧院”,action =“视图”,id = 2}。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟柳画桥 2024-08-24 21:41:59

区域与路由没有直接关系,因此您的问题变成“路由是否支持子域?”

不幸的是,答案是没有对此的内置支持。

然而,好消息是,许多人已经尝试并成功使用基于路由构建的自定义代码:

如果您弄清楚如何在不使用路由的情况下路由子域然后用区域来做应该不会更困难。转到您的区域注册上下文并使用您在其中选择的任何方法。区域只是将控制器和视图组合在一起的一种方式 - 它们对 ASP.NET MVC 本身没有太多内在意义(它们做了一点点,但不是很多)。

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

街角卖回忆 2024-08-24 21:41:59

您可以使用 IIS7 URL 重写模块轻松将域或子域映射到 MVC2 区域。以下是将子域映射到两个区域的两个简单规则。我添加了一个限制,如果请求是针对实际文件(即 CSS、JS 或图像文件),则不使用此规则。

以下是 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>

您必须更改以下规则才能适合您的特定用例。特别是如果您需要使用根区域上的控制器。在这种情况下,只需添加条件或创建新规则即可。

下载 IIS URL 重写模块(必需):
http://learn.iis.net/page。 aspx/460/using-the-url-rewrite-module/

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/

风吹雨成花 2024-08-24 21:41:59

我遇到了同样的问题,并且构建了一个适合我的解决方案,它位于我的 博客,希望对您有用。

干杯
托尼

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

吻风 2024-08-24 21:41:59

我已经尝试了其他线程中提到的很多解决方案,发现事情很快变得太复杂。 ASP.Net MVC 似乎希望您对 Route 进行子类化以执行这种高级路由,但它似乎对我不起作用。我永远无法将域映射到名称空间,因此我遇到了“不明确的控制器”异常(因为我在两个名称空间中都有一个主控制器)。

最终我使用了一个约束将子域指向名称空间。

这是我的路线。请注意,该路由适用于“api”。子域:

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

这是上面引用的“SubdomainRouteConstraint”类:

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

这显然是一个黑客,但我对它最终的简单程度感到非常满意。

您可以轻松地调整此代码以动态地将子域映射到一个区域,但我只有两个区域,因此我只需分别注册每个区域。另外,这让我可以自由地在每个区域内设置不同的路由。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文