MVC中的响应永久重定向未发出301或未更改URL [英] Response Permanent Redirect in MVC not issuing 301 or changing URL

查看:151
本文介绍了MVC中的响应永久重定向未发出301或未更改URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将旧的ASP.NET Web窗体站点转换为ASP.NET MVC5.我想对旧页面URL发出永久重定向.

I am converting an old ASP.NET web forms site to ASP.NET MVC 5. I would like to issue permanent redirects for the old page URLs.

这就是我所做的-

RouteConfig.cs:

RouteConfig.cs:

routes.MapRoute("About_old", 
      "About/About.aspx", 
      new { controller = "Home", action = "About_old" });

HomeController.cs:

HomeController.cs:

public ActionResult About_old()
{
   return new RedirectResult("/About", true);

   // I've also tried 
   // return RedirectToActionPermanent("About"); 
   // return RedirectPermanent("/About");
}

所有尝试都会加载正确的/About视图,但是URL不会更改,并且在响应中看不到301代码.换句话说,URL是"localhost/About/About.aspx",我希望它是"localhost/About"

All attempts load the correct /About view, however the URL does not change, and I do not see a 301 code in the response. In other words, the URL is "localhost/About/About.aspx" and I expect it to be "localhost/About"

完成来自Chrome的请求/回复:

Complete Request/Repsonse from Chrome:

Request URL:http://localhost:55774/About/About.aspx
Request Method:GET
Status Code:200 OK

Request Headers
GET /About/About.aspx HTTP/1.1
Host: localhost:55774
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

Response Headers
Cache-Control:private
Content-Encoding:gzip
Content-Length:2284
Content-Type:text/html; charset=utf-8
Date:Sat, 01 Mar 2014 18:10:41 GMT
Server:Microsoft-IIS/8.0
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.1
X-Powered-By:ASP.NET

我在哪里都看不到301,并且URL保持不变.我必须认为这与我在RouteConfig.cs中映射旧aspx页面的路由有关,因为所有操作方法都具有相同的结果. 注意我在下面放置了一个使用global.asax的解决方案,但是我希望它可以像上面所尝试的那样工作,因此我没有接受我的回答.

Nowhere do I see a 301 and the URL does not change. I have to think this has to do with how I am mapping the route of the old aspx page in RouteConfig.cs as all action methods have the same results. NOTE I have put a solution using global.asax below, however I would prefer it to work as I am attempting above, so I have not accepted my answer.

我是在做错什么还是只是错过了什么?如何获得301签发和更改URL的地址?

Am I doing something wrong or just missing something? How do I get the 301 to issue and URL to change?

推荐答案

这是我的解决方案(Global.asax)

Here is my solution (Global.asax)

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
        {
            string currentUrl = HttpContext.Current.Request.Path.ToLower();
            if (currentUrl.EndsWith("/about/about.aspx"))
            {
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", "/About");
                Response.End();
            }
        }

来自此处的答案:从域到www.domain的全局301重定向

这篇关于MVC中的响应永久重定向未发出301或未更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆