开发者

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

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.do开发者_高级运维mainB.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


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


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 */));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜