C# Response.Redirect HTTP 状态码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1723487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:16:50  来源:igfitidea点击:

Response.Redirect HTTP status code

c#asp.nethttpredirectasp-classic

提问by Vinz

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?

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

采纳答案by rick schott

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

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

ASP.NET 4.0 is has the RedirectPermanentmethod for that if needed.

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

回答by Heinzi

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

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

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 realthis-page-moved-to-another-URL-forever scenario (where a return code of 301 would be appropriate).

在这些情况下,301 将是完全错误的。事实上,我认为上述情况(在一些 UI 交互后有条件地将用户移动到另一个页面)Response.Redirect真正的this-page-moved-to-another-URL-forever 场景(返回301 的代码是合适的)。

回答by AnthonyWJones

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.

除了 Heinzi 的回答之外,网络上唯一可能会关注 301 的实体是搜索引擎。大多数浏览器不会跟踪和记录 301,以便自动重定向对初始 URL 的任何后续请求。浏览器对待 301 的方式与对待 302 的方式相同。因此,在 ASP.NET 中生成的动态内容中使用 302 是非常合适的。

回答by LaJmOn

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

回答by Bubai Hazra

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

您得到的错误不是由于 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.

HTTP 响应状态代码 301 Moved Permanently 用于永久重定向,这意味着使用收到 301 Moved Permanently 响应的 URL 的当前链接或记录应更新为响应的 Location 字段中提供的新 URL。