如何强制用于ASP.NET MVC网站单个域? [英] How to force a single domain to be used for ASP.NET MVC site?

查看:142
本文介绍了如何强制用于ASP.NET MVC网站单个域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个具有两个域指向它的一个ASP .NET MVC的网站(例如www.domainA.com和www.domainB.com)。我想改变我所有的链接,使用www.domainA.com即使访问www.domainB.com。

I currently have an ASP .NET MVC site that has two domains pointing to it (e.g. www.domainA.com and www.domainB.com). I would like to change all of my links to use www.domainA.com even if www.domainB.com is visited.

所有我的网址都是由产生内置Url.Action和Html.ActionLink方法。

All of my urls are generated by the built-in Url.Action and Html.ActionLink methods.

我也跟着在<一的说明href=\"http://stackoverflow.com/questions/7685738/asp-net-mvc-change-domain-used-by-url-action\">$p$pvious问题但我不能完全得到它的工作。

I followed the instructions in a previous question but I can't quite get it working.

这里是我的自定义路由

public class MultipleDomainRoute : System.Web.Routing.Route
{
    public MultipleDomainRoute(string url, IRouteHandler routeHandler)
        : base(url, routeHandler)
    {
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        VirtualPathData path = new VirtualPathData(this, "http://www.domainA.com/" + values["controller"] + "/" + values["action"]);
        return path;
    }
}

这里是我的自定义路由处理程序:

class MyRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MvcHandler(requestContext);
    }
}

这里是我的RegisterRoutes方法:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new MultipleDomainRoute("{controller}/{action}/{id}", new MyRouteHandler()));

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );
}

这条路线是工作(即不撞毁我的网站),但是当我运行它,我的网址只是附加到的http://本地主机 ...例如,我得到的http://本地主机:56822 / HTTP:/www.domainA.com/Home/Index。

The route is working (i.e. not crashing my site) but when I run it, my urls are just appended to http://localhost... For example, I get "http://localhost:56822/http:/www.domainA.com/Home/Index".

没有人有任何想法,为什么这是不工作?

Does anyone have any idea why this is not working?

推荐答案

如果您正在使用IIS 7,它可能是更好的实现这个网络服务器级别

If you're using IIS 7, it may be better to implement this at web server level.

这样,正确的状态code将在HTTP头返回给浏览器,你的MVC应用程序可以处理自己的路由,而不必担心域名和主机名,在大多数情况下,是网络服务器的工作。

This way, the proper status code will be returned to the browser in the HTTP header and your MVC app can deal with its own routing without having to worry about domains and hostnames which, in most cases, is the web server's job.

有两种方法来实现这个

1。通过IIS管理控制台

打开你的网站在IIS控制台,并选择URL重写,并按照下列步骤操作:

Open your website in the IIS console and select URL Rewrite and follow these steps:


  1. 创建一个新的规则,并设置匹配的URL通配符和输入模式作为你的二级域名

  2. 添加一个条件下的 {} HTTP_HOST 等于网站的二级域名。

  3. 将动作类型重定向,然后输入你想重定向到的格局。 http://mysite.com {R:1}在这种情况下将映射之后的所有IP地址到其域equivilent,所以mysite.net/mycontent会重定向到mysite.com/mycontent~~V

  1. Create a new rule and set the match URL to wildcard and enter the pattern as your secondary domain
  2. Add a condition where {HTTP_HOST} is equal to the site's secondary domain.
  3. Set the "action type" to redirect, and enter the pattern you wish to redirect to. "http://mysite.com{R:1}" in this case would map everything after the IP address to its domain equivilent, so mysite.net/mycontent would redirect to mysite.com/mycontent

2。在你的web.config

<system.webServer>
<rewrite>
 <rules>
 <rule name="Redirect to mydomain.com" stopProcessing="true">
 <match url=".*"/>
 <conditions>
 <add input="{HTTP_HOST}" pattern="^(www.)?mydomain.net"/>
 </conditions>
 <action type="Redirect" url="http://www.mydomain.com/{R:0}"/>
 </rule>
 </rules>
 </rewrite>
</system.webServer>

此方法将确保您的子目录映射和你的网站仍然是搜索引擎友好,与正在产生应有的反应codeS。

This approach will ensure your subdirectories are mapped and your site remains search engine friendly, with proper response codes being generated.

这篇关于如何强制用于ASP.NET MVC网站单个域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆