Route www link to non-www link in .net mvc

前端 未结 3 1078
太阳男子
太阳男子 2021-01-01 04:40

It seems with the built in friendly routing library in .NET MVC, it would allow us to do something like this.

In case it\'s not obvious what I want to with the built

3条回答
  •  别那么骄傲
    2021-01-01 05:34

    try to add this into your Global.asax :

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://YourSite.com"))
        {
                HttpContext.Current.Response.Status = "301 Moved Permanently";
                HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://YourSite.com","http://www.YourSite.com"));
        }
    

    it works and tested.

提交回复
热议问题