简体   繁体   中英

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM