简体   繁体   English

如何在ASP.NET应用程序中发送HTTP标头时如何判断?

[英]How can I tell when HTTP Headers have been sent in an ASP.NET application?

Long story short, I have an ASP.NET application I'm trying to debug and at some point, in very particular circumstances, the application will throw exceptions at a Response.Redirect() stating: 简而言之,我有一个ASP.NET应用程序,我正在尝试调试,在某些情况下,在非常特殊的情况下,应用程序将在Response.Redirect()处抛出异常,说明:

"Cannot redirect after HTTP headers have been sent."

Which I more or less get, except that I cannot figure out where the headers were sent. 我或多或少得到,但我无法揣摩出头被送到。

Is there something to look for in an ASP.NET application that will indicate that the HTTP headers have been sent? 是否有一些东西要在ASP.NET应用程序中查找,表明HTTP标头已被发送?

BONUS DIFFICULTY: The ASP.NET app is still in .NET 1.1. 奖金难度: ASP.NET应用程序仍在.NET 1.1中。 The circumstances regarding the delay behind the upgrade are a really sore subject. 关于升级延迟的情况是一个非常痛苦的主题。

HttpApplication has an event PreSendRequestHeaders which is called just when headers are writtne. HttpApplication有一个事件PreSendRequestHeaders ,只有当头文件被写入时才会调用它。 Subscribe to this and log it or add a breakpoint. 订阅此并记录或添加断点。

Beyond that, HttpResponse has a internal property called HeadersWritten ( _headersWritten field in .NET 1.1). 除此之外, HttpResponse有一个名为HeadersWritten内部属性(.NET 1.1中的_headersWritten字段)。 Since it's internal you can't access it directly, but you can through reflection. 由于它是内部的,你不能直接访问它,但你可以通过反射。 If this is only for internal debugging (ie, not production code), then it's be fine to use reflection. 如果这仅用于内部调试(即非生产代码),那么使用反射就可以了。

Check this method before/after all the page lifecylce events. 在所有页面生命周期事件之前/之后检查此方法。 Once you know which event is writing out the headers, add more HeadersWritten checks to find out where they're getting written. 一旦知道哪个事件正在写出标题,添加更多HeadersWritten检查以找出它们写入的位置。 Through progressive narrowing of checks to this property, you'll find it. 通过逐步缩小对该物业的支票,您将找到它。

New info 新信息

HeadersWritten property is public starting from .Net 4.5.2 HeadersWritten属性是从.Net 4.5.2开始公开的

Samuel's reply just solved this problem for me (+1). 塞缪尔的回答刚刚为我解决了这个问题(+1)。 I can't paste a code sample in a comment, but in the interest of helping others, here's how I used the event he suggested to add a HeadersWritten property to my IHTTPHandler: 我无法在评论中粘贴代码示例,但为了帮助其他人,我在这里使用他建议的事件来向我的IHTTPHandler添加HeadersWritten属性:

protected bool HeadersWritten { get; private set; }

void ApplicationInstance_BeginRequest(object sender, EventArgs e)
{
    HeadersWritten = false;
}

void ApplicationInstance_PreSendRequestHeaders(object sender, EventArgs e)
{
    HeadersWritten = true;
}

public void ProcessRequest(HttpContextBase context)
{
    context.ApplicationInstance.PreSendRequestHeaders += new EventHandler(ApplicationInstance_PreSendRequestHeaders);
    do_some_stuff();
}

In my code that would break if I mess with headers too late, I simply check the HeadersWritten property first: 在我的代码中,如果我把标题搞得太晚会破坏,我只需先检查HeadersWritten属性:

if (!HeadersWritten)
{
    Context.Response.StatusDescription = get_custom_description(Context.Response.StatusCode);
}

暂无
暂无

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

相关问题 ASP.NET 3.5,googlebot,301重定向“在发送HTTP标头后无法重定向” - ASP.NET 3.5, googlebot, 301 Redirect “Cannot redirect after HTTP headers have been sent” 当托管在Azure应用程序服务中时,如何控制添加到ASP.NET核心Web应用程序中的HTTP响应标头? - How can I control the HTTP response headers added to my ASP.NET core web application when hosted in Azure app service? asp.net网站-是否可以预编译? - asp.net website - Can I tell if it has been precompiled? 我可以清除发送的标题吗? 在ASP.NET上 - Can I clear headers that sent? on ASP.NET 如何完全控制ASP.Net WebAPI 2中的HTTP标头? - How can I get a full control over the HTTP headers in ASP.Net WebAPI 2? 如何确定Outlook中是否已发送会议邀请? - How can you tell if invitations have been sent for a meeting in Outlook? 在表上放置约束后,如何删除asp.net身份用户? - How do I remove an asp.net identity user when constraints have been placed on the table? 在所有ASP.NET启动脚本执行完毕后,如何执行某些Javascript? - How can I execute some Javascript after all ASP.NET startup scripts have been executed? 发送HTTP标头后,服务器无法清除标头 - Server cannot clear headers after HTTP headers have been sent 当应用程序通过 http 请求运行时,我可以添加和删除运行状况检查吗(asp.net 核心) - Can I add and remove health checks when application is running via a http request (asp.net core)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM