Response.Redirect HTTP status code

前端 未结 5 1599
梦如初夏
梦如初夏 2020-12-06 00:10

Why is it that ASP/ASP.NET Response.Redirect uses a HTTP-302 status code (\"Moved Temporarily\") even though in most cases a HTTP-301 status code (\"Moved Permanently\") wou

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 01:03

    One common use case of Response.Redirect is to move the user to another page in server-side code after a postback, e.g. something along the lines of

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

    In those cases, 301 would be completely wrong. In fact, I think that the above case (conditionally move the user to another page after some UI interaction) is a much more common use of Response.Redirectthan a real this-page-moved-to-another-URL-forever scenario (where a return code of 301 would be appropriate).

提交回复
热议问题