为什么ASP.NET Response.Redirect使用HTTP-302状态代码(“临时移动”),而在大多数情况下,HTTP-301状态代码(“永久移动”)更合适?
发布于 2009-11-13 00:39:34
状态为301的响应应该是可缓存的,我认为大多数ASP/ASP.NET重定向都不希望出现这种行为。
如果需要的话,ASP.NET 4.0提供了RedirectPermanent方法。
发布于 2009-11-13 01:01:24
我已经成功地使用了这个方便的永久重定向:
public void RedirectPermanent(string newPath)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", newPath);
HttpContext.Current.Response.End();
}发布于 2009-11-13 00:35:37
Response.Redirect的一个常见用例是在回发之后将用户移动到服务器端代码中的另一个页面,例如
private void MyButton_Click(object sender, EventArgs e)
{
if (some condition) {
Response.Redirect("ShowProduct.aspx");
} else {
Response.Redirect("SorryOutOfStock.aspx");
}
}在这种情况下,301将是完全错误的。事实上,我认为上面的情况(在一些UI交互之后有条件地将用户移动到另一个页面)是Response.Redirect的一种更常见的用法,而不是真正的这个页面永远移动到另一个URL的场景(返回代码301比较合适)。
https://stackoverflow.com/questions/1723487
复制相似问题