问题
I have a website that has two domains pointing to the same content. Let's call them www.domainA.com and www.domainB.com where www.domainA.com/Page is the same as www.domainB.com/Page.
Every page on the site has a number of common navigation links (and others) that are constructed using a mixture of Url.Action and Html.ActionLink calls. The resulting urls are based on the current domain. Because www.domainA.com is the primary domain, I would like any links generated from www.domainB.com to be based on www.domainA.com.
Can this be done centrally, rather than me going around the whole site and hard-coding it?
Thanks, Alan
回答1:
I ended up using the solution here to instruct IIS to rewrite requests to my old domain.
Hope this helps others with the same requirement.
Alan
回答2:
You can fix this by having custom route objects, that inherit from System.Web.Routing.Route
.
For instance:
public class MultipleDomainRoute : System.Web.Routing.Route
{
// ...
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData path = base.GetVirtualPath(requestContext, values);
if (requestContext.Url.Host == "domain2.com") {
path.VirtualPath = "http://domain1.com" + path.VirtualPath;
}
return path;
}
}
Then in your global.asax, where you register your routes use:
routes.Add(new MultipleDomainRoute(/* args */));
来源:https://stackoverflow.com/questions/7685738/asp-net-mvc-change-domain-used-by-url-action