ASP.NET Response.Redirect uses 302 instead of 301

前端 未结 4 1438
不知归路
不知归路 2020-12-28 14:00

using the following code

context.Response.StatusCode = 301;

context.Response.Redirect(newUrl, true);
context.Response.End();

I can see in

4条回答
  •  猫巷女王i
    2020-12-28 14:30

    Response.Redirect() will overwrite the StatusCode property with the code for a redirect (302). Also, because you're using the Response.Redirect() overload taking the boolean parameter, you should set it to False if you want to call Response.End() yourself. Otherwise it's redundant and can cause errors.

    Try the following (pre-ASP.NET 4.0; Adam Butler's answer covers the new best practice):

    context.Response.Redirect(newUrl, false);
    context.Response.StatusCode = 301;
    context.Response.End();
    

提交回复
热议问题