Fork me on GitHub

让Response.Redirect页面重定向更有效率

用 Redirect 方法可将浏览器重定向到另一个 URL,而不是将内容发送给用户。 这里有一篇文章介绍使用Redirect《Using Response.Redirect Effectively》 ,文章详细的讨论了Response.Redirect ,给出了一段代码:

public static class HttpResponseExtensions  
{  
         public static void RedirectUser(this HttpResponse response, string url)  
         { 
             if (response.IsRequestBeingRedirected)  
                  return;  
             response.Redirect(url, false);  
             var context = HttpContext.Current;  
             if (context != null)
         {  
                   context.ApplicationInstance.CompleteRequest();  
             }  
         }  
}  
另外ASP.NET 4 增加了一个RedirectPermanent方法,该方法同样是重定向,但生成的HTTP响应状态不是上边所演示的302,而是301(永久跳转),301 是对搜索引擎最友好的重定向方式。你有个网站http://www.cnblogs.com ,当人们访问http://www.cnblogs.com 这个URL时,你就把他们重定向到http://www.cnblogs.com/shanyou/,那么当搜索引擎爬到http:www.cnblogs.com这个网址时,如果它不能很好地跟随重定向,则它将认为http://www.cnblogs.com页面时没有内容的,所以这个页面的排名将会非常靠后。 如果我们把一个地址采用301 跳转方式跳转的话,搜索引擎会把老地址的PageRank等信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。 
posted @ 2013-10-27 07:30  张善友  阅读(7896)  评论(1编辑  收藏  举报