如何强制 ASP.NET MVC 站点使用单个域?
我目前有一个 ASP .NET MVC 站点,有两个域指向它(例如 www.domainA.com 和 www.domainB.com)。我想更改所有链接以使用 www.domainA.com,即使访问了 www.domainB.com 也是如此。
我的所有 url 都是由内置的 Url.Action 和 Html.ActionLink 方法生成的。
我按照上一个问题中的说明进行操作,但我不能完全让它工作。
这是我的自定义路由:
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://localhost...例如,我得到“<一个href="http://localhost:56822/http:/www.domainA.com/Home/Index" rel="nofollow noreferrer">http://localhost:56822/http:/www.domainA.com/Home/索引”。
有谁知道为什么这不起作用?
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.
All of my urls are generated by the built-in Url.Action and Html.ActionLink methods.
I followed the instructions in a previous question but I can't quite get it working.
Here is my custom route:
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;
}
}
Here is my custom route handler:
class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MvcHandler(requestContext);
}
}
Here is my RegisterRoutes method:
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
);
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 IIS 7,最好在Web 服务器级别实现此功能。
这样,正确的状态代码将在 HTTP 标头中返回到浏览器,并且您的 MVC 应用程序可以处理自己的路由,而不必担心域和主机名,这在大多数情况下是 Web 服务器的工作。
有两种方法可以实现这一目标
1。通过 IIS 管理控制台
在 IIS 控制台中打开您的网站并选择 URL 重写,然后按照以下步骤操作:
2。在您的 web.config 中,
此方法将确保您的子目录已映射,并且您的网站保持搜索引擎友好,并生成正确的响应代码。
If you're using IIS 7, it may be better to implement this at web server level.
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.
There are two ways to achieve this
1. Via the IIS administration console
Open your website in the IIS console and select URL Rewrite and follow these steps:
2. In your web.config
This approach will ensure your subdirectories are mapped and your site remains search engine friendly, with proper response codes being generated.
为什么不直接将domainB转发到domainA?我相信大多数注册商都会让您将一个域名转发到另一个域名。
Why not just forward domainB to domainA? I believe most registrars let your forward one domain to another.
有几种方法可以做到这一点。
在 IIS7 中,正如其他人提到的,您可以使用内置的 URL 重写模块轻松完成此操作。请参阅
http://www.dotnetexpertguide.com/2011 /08/iis-7-redirect-domaincom-to.html
或者,将类似的内容添加到您的 web.config 中:(
来自 这个site)
在 IIS6 中,只需为辅助域添加一个新站点,然后在“主目录”选项卡中将其设置为指向您的主域:
这将导致对第二个站点/辅助域的主机标头的所有请求重定向到主域,包括原始站点中包含的所有路径和 url 参数。
请注意,如果您不使用这些顶级选项之一,则可能会遇到 cookie 问题。浏览器不喜欢将 cookie 从“www.a.com”发送到“www.b.com”。
There are a couple of ways to do this.
In IIS7, as others have mentioned, you can do this pretty easily with the built-in URL Rewrite module. See
http://www.dotnetexpertguide.com/2011/08/iis-7-redirect-domaincom-to.html
Or, add something like this to your web.config:
(from this site)
In IIS6, just add a new site for the secondary domain, and in the "Home Directory" tab set it to point to your primary domain:
That'll cause all requests to the host headers for the second site/secondary domain to redirect to the primary domain, including whatever paths and url parameters were included with the original site.
Note that if you don't use one of these top-level options, you'll probably have issues with cookies. Browsers don't like to send cookies from "www.a.com" to "www.b.com".