简体   繁体   English

ASP.NET 3.5,googlebot,301重定向“在发送HTTP标头后无法重定向”

[英]ASP.NET 3.5, googlebot, 301 Redirect “Cannot redirect after HTTP headers have been sent”

I have set up a dynamic 301 redirect routine within a custom HttpModule. 我在自定义HttpModule中设置了动态301重定向例程。 The code accepts the incoming url, parses the querystring, and using a config and app specific logic, redirects to a new SEO friendly url using the following code: 代码接受传入的URL,解析查询字符串,并使用配置和应用程序特定的逻辑,使用以下代码重定向到新的SEO友好URL:

if (HttpContext.Current.Response.IsRequestBeingRedirected)
    return;

if (!HttpContext.Current.Response.IsClientConnected)
{
    response.End();
    return;
}

response.Redirect(newLocation, false);
response.Status = "301 Moved Permanently";
response.StatusCode = 301;

This works fine and dandy if you enter a legacy url directly in a web browser. 如果您直接在Web浏览器中输入旧URL,这可以正常工作。 However, my event log is showing a bunch of "Cannot redirect after HTTP headers have been sent" HttpExceptions when the same url is accessed by the googlebot (66.249.71.11). 但是,当googlebot(66.249.71.11)访问相同的URL时,我的事件日志显示了一堆“在发送HTTP标头后无法重定向”HttpExceptions。

I'm at a loss as to what the issue is and how to resolve it. 我对这个问题是什么以及如何解决它感到茫然。

In ASP.NET 3.5 Response.Redirect automatically sends a 302 (rather than a 301) and terminates the connection with the client. 在ASP.NET 3.5中,Response.Redirect自动发送302(而不是301)并终止与客户端的连接。 If you want to use a 301 then you have to manually insert all of the headers. 如果要使用301,则必须手动插入所有标题。 Something like the following: 类似于以下内容:

Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","/about.aspx");
Response.End();

Alternatively you can upgrade to ASP.NET 4.0 where there is now a method to indicate something has moved permanently. 或者,您可以升级到ASP.NET 4.0,现在有一种方法可以指示某些内容已永久移动。

Response.RedirectPermanent("/about.aspx");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActionFilter - 发送 HTTP 标头后无法重定向 - ActionFilter - Cannot redirect after HTTP headers have been sent 发送HTTP标头后无法重定向 - Cannot redirect after HTTP headers have been sent ASP.NET MVC 在 HTTP 标头后无法重定向 - ASP.NET MVC Cannot redirect after HTTP headers HTTPS重定向导致错误“在发送HTTP标头后,服务器无法附加标头” - HTTPS Redirect Causing Error “Server cannot append header after HTTP headers have been sent” “在发送HTTP标头后无法重定向。”使用HttpStatusCode.Unauthorized返回HttpResponseMessage时 - “Cannot redirect after HTTP headers have been sent.” When returning HttpResponseMessage with HttpStatusCode.Unauthorized “发送HTTP标头后无法重定向”,但是如果删除RedirectToAction,Response.Redirect将不起作用 - “Cannot redirect after http headers have been sent”, but Response.Redirect doesn't work if I remove RedirectToAction 发送HTTP标头后,服务器无法清除标头 - Server cannot clear headers after HTTP headers have been sent 如何在ASP.NET应用程序中发送HTTP标头时如何判断? - How can I tell when HTTP Headers have been sent in an ASP.NET application? .NET,ASHX,“发送PDF Streem后,服务器无法在发送HTTP标头后追加标头” - .NET, ASHX, “Server cannot append header after HTTP headers have been sent” after sending PDF Streem 在发送 HTTP 标头后,服务器无法 append header - Server cannot append header after HTTP headers have been sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM