[英]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.