2009-07-05 85 views
2

我需要永久重定向某些页面,并将用户重定向到新的URL。如何使用ASP.NET发送301永久重定向?

此代码只设置正确的标题。用户没有重定向。

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

+0

我只是写了一个博客帖子:http://www.ko-sw.com/Blog/post/Permanent-Redirect-Using-ASPNET.aspx – Kerido 2010-02-22 21:19:12

回答

4

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

+0

到Response.End的伎俩:) – MartinHN 2009-07-05 02:15:08

0

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

2

请注意,在ASP.NET 4.0中,现在内置了这个功能,因此您可以使用RedirectPermanent()方法。例如

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