Response.Redirect HTTP状态代码

Vin*_*inz 20 c# asp.net redirect http asp-classic

为什么ASP/ASP.NET Response.Redirect使用HTTP-302状态代码("暂时移动"),即使在大多数情况下HTTP-301状态代码("永久移动")更合适?

ric*_*ott 41

状态301的响应应该是可缓存的,我认为您不希望大多数ASP/ASP.NET重定向的行为.

如果需要,ASP.NET 4.0具有RedirectPermanent方法.

  • +1告诉我们RedirectPermanent. (8认同)

LaJ*_*mOn 19

我已经成功使用了这个方便的永久重定向:

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();
}
Run Code Online (Sandbox Code Playgroud)


Hei*_*nzi 9

一个常见的用例Response.Redirect是在回发后将用户移动到服务器端代码中的另一个页面,例如,某些内容

private void MyButton_Click(object sender, EventArgs e)
{
    if (some condition) {
         Response.Redirect("ShowProduct.aspx");
    } else {
         Response.Redirect("SorryOutOfStock.aspx");
    }
}
Run Code Online (Sandbox Code Playgroud)

在这些情况下,301将是完全错误的.事实上,我认为上述情况(在一些UI交互之后有条件地将用户移动到另一个页面)Response.Redirect比一个真正的这个页面移动到另一个URL永远的场景(其中返回)更常见的用途代码301是合适的).