How can I send a 301 Permanent Redirect with ASP.NET?

空扰寡人 提交于 2019-12-10 10:57:18

问题


I need to permanent redirect some pages, and redirect the user to the new URL as well.

This code only sets the correct headers. The user are not redirected.

public static void PermanentRedirect(this HttpResponse response, string newUrl)
{
  response.Status = "301 Moved Permanently";
  response.StatusCode = 301;
  response.AddHeader("Location", newUrl);
}

If I put:

Response.Redirect(newUrl);

at the end, a 302 Temporary Redirect is performed.

How can I 301 redirect the user?

Related Questions:

How do I programatically 301 redirect in an asp page


回答1:


Try Response.Flush and Response.End. Redirect says to end the request by sending a 302.




回答2:


Note in ASP.NET 4.0 this is now built-in so you can use the RedirectPermanent() method. e.g.

RedirectPermanent("/newpath/foroldcontent.aspx"); 



回答3:


or maybe try ISAPI? it mimicks mod_rewrite and other .htaccess functionality on IIS.



来源:https://stackoverflow.com/questions/1083337/how-can-i-send-a-301-permanent-redirect-with-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!