Server cannot add header after sending HTTP headers

I get this intermittent exception in my asp.net C # web application:

The server cannot add the header after sending the HTTP headers.

And this is due to the fact that the application adds content to the response header of the pages after the page is sent. I'm not sure why its intermittent, but what I need to do is check before changing the headers to check if the page has been sent.

Does anyone know how I can achieve such a check?

+7
source share
1 answer

There are two ways to do this:

  • Subscribe to the PreSendRequestHeaders of HttpApplication and assume the headers are sent at this point. Set the flag in context and check it everywhere

  • Ugly solution: HttpResponse has an internal property called HeadersWritten . Since it is internal, you will have to access it through reflection. I would recommend only using this for debugging. Check this before / after all the events of the lifecyle page and find out where the problem is. Do not leave this in working code

+9
source

All Articles