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

后端 未结 2 1615
情话喂你
情话喂你 2021-02-19 03:39

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 Respon

2条回答
  •  温柔的废话
    2021-02-19 04:03

    HttpApplication has an event PreSendRequestHeaders which is called just when headers are writtne. 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). Since it's internal you can't access it directly, but you can through reflection. If this is only for internal debugging (i.e., 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. Through progressive narrowing of checks to this property, you'll find it.

    New info

    HeadersWritten property is public starting from .Net 4.5.2

提交回复
热议问题