English 中文(简体)
Response.Redirect HTTP status code
原标题:

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") would be more appropriate?

最佳回答

Responses with status 301 are supposed to be cacheable, and I don t think you want that behavior for most ASP/ASP.NET redirects.

ASP.NET 4.0 is has the RedirectPermanent method for that if needed.

问题回答

I ve used this handy Permanent Redirect with success:

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();
}

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).

In addition to the answer from Heinzi, the only entity on the web that is likely to take much notice of the 301 would be the search engines. Most browsers will not track and record 301 in order automatically redirect any subsequent request for the initial URL. Browsers treat 301 identically to how they treat 302. Hence 302 in dynamic content such as generated in ASP.NET is quite appropriate.

The error you are getting is not due to response.redirect !

The HTTP response status code 301 Moved Permanently is used for permanent redirection, meaning current links or records using the URL that the 301 Moved Permanently response is received for should be updated to the new URL provided in the Location field of the response.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签