我该如何在ASP.NET中进行永久重定向? 我想将一个页面永久重定向到另一个页面,使用301重定向。
protected void Page_PreInit(object sender, EventArgs e)
{
Response.StatusCode = 301;
Response.StatusDescription = "Moved Permanently";
Response.RedirectLocation = "AnotherPage.aspx";
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
在4.0版本中,有一个简单的HttpResponse.RedirectPermanent()
方法为你做了上述所有工作:
Response.RedirectPermanent("AnotherPage.aspx");
Response.RedirectPermanent("AnotherPage.aspx");
来自ASP.NET 4.0和Visual Studio 2010 Web Development Beta 1概述白皮书:
在Web应用程序中,常见做法是随着时间的推移移动页面和其他内容,这可能会导致搜索引擎中积累过时的链接。在ASP.NET中,开发人员通常使用Response.Redirect方法处理对旧URL的请求,将请求转发到新URL。然而,Redirect方法发出HTTP 302 Found(临时重定向)响应,当用户尝试访问旧URL时会导致额外的HTTP往返。
ASP.NET 4.0添加了一个新的RedirectPermanent帮助器方法,使得轻松发出HTTP 301 Moved Permanently响应成为可能。
<system.webServer>
<rule name="Redirect Source to Destination" stopProcessing="true">
<match url="/source.aspx" />
<action type="Redirect" url="/destination.aspx" redirectType="Permanent" />
</rule>
</system.webServer>