如何使用ASP.NET发送301永久重定向?

时间:2009-07-05 02:02:45

标签: asp.net http redirect http-status-code-301 permanent

我需要永久重定向某些页面,并将用户重定向到新的URL。

此代码仅设置正确的标头。用户未被重定向。

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

如果我把:

Response.Redirect(newUrl);

最后,执行302临时重定向。

我如何301重定向用户?

  

相关问题:

     

How do I programatically 301 redirect in an asp page

3 个答案:

答案 0 :(得分:4)

尝试Response.Flush和Response.End。重定向表示通过发送302来结束请求。

答案 1 :(得分:2)

请注意,在ASP.NET 4.0中,这是内置的,因此您可以使用RedirectPermanent()方法。 e.g。

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

答案 2 :(得分:0)

或者尝试ISAPI?它模仿IIS上的mod_rewrite和其他.htaccess功能。