Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC中的响应永久重定向未发出301或更改URL_Asp.net Mvc_Redirect_Asp.net Mvc 5 - Fatal编程技术网

Asp.net mvc MVC中的响应永久重定向未发出301或更改URL

Asp.net mvc MVC中的响应永久重定向未发出301或更改URL,asp.net-mvc,redirect,asp.net-mvc-5,Asp.net Mvc,Redirect,Asp.net Mvc 5,我正在将旧的ASP.NET web表单网站转换为ASP.NET MVC 5。我想为旧页面URL发出永久重定向 这就是我所做的- RouteConfig.cs: routes.MapRoute("About_old", "About/About.aspx", new { controller = "Home", action = "About_old" }); HomeController.cs: public ActionResult About_old() {

我正在将旧的ASP.NET web表单网站转换为ASP.NET MVC 5。我想为旧页面URL发出永久重定向

这就是我所做的-

RouteConfig.cs:

routes.MapRoute("About_old", 
      "About/About.aspx", 
      new { controller = "Home", action = "About_old" });
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”

从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提供了一个解决方案,但是我更希望它能像我在上面尝试的那样工作,所以我没有接受我的答案

我是做错了什么还是错过了什么?我如何获得要发布的301和要更改的URL?

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


从这里的答案来看:

我认为你的问题应该是“为什么不发送301”。。。而且不复制2010年的代码,比永久重定向还早2个月。虽然有效,但不需要您提供的解决方案作为答案。现在您一直在执行该操作,而不是在您需要的页面上执行。RedirectPermanent不会更改URL。这就是我需要做的。如果你能告诉我如何使用重定向永久,我将接受你的回答。MikeSmithDev-根据你的建议更新问题
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();
            }
        }