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

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

问题描述

我正在将旧的 ASP.NET Web 表单站点转换为 ASP.NET MVC 5.我想为旧页面 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天全站免登陆