ASP .NET MVC - Change domain used by URL.Action?

試著忘記壹切 提交于 2019-12-25 11:51:55

问题


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

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