Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

"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?

BONUS DIFFICULTY: The ASP.NET app is still in .NET 1.1. The circumstances regarding the delay behind the upgrade are a really sore subject.

like image 241
Tom Kidd Avatar asked Mar 04 '10 19:03

Tom Kidd


People also ask

What is date header in HTTP response?

The Date property represents the value of a Date HTTP header on an HTTP response. The Date header is the date and time the message was sent.

How do I get the response header location?

To check this Location in action go to Inspect Element -> Network check the response header for Location like below, Location is highlighted you can see.

Do HTTP responses have headers?

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age , Location or Server are used to give a more detailed context of the response.


1 Answers

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

like image 180
Samuel Neff Avatar answered Oct 20 '22 12:10

Samuel Neff