15

I am adding headers to a page as follows: Page.Response.AddHeader("foo", "bar");

Depending upon previous processing, sometimes this fails with "Server cannot append header after HTTP headers have been sent." I am dealing with this by enclosing Page.Response.AddHeader("foo", "bar"); within a try-catch construct. However, to keep things cleaner and avoid generating an exception is there any way to detect that the headers have already been sent? (btw if I try evaluating Page.Response.Headers then I get the following error: "This operation requires IIS integrated pipeline mode")

Thanks

2
  • For caching use Page.Response.Cache Commented Sep 21, 2010 at 8:01
  • I have seen this before. You need to explain what technologies you are using: caching, AJAX, MVC, ... and where in the code you are adding this header so we can help. Commented Sep 21, 2010 at 8:34

4 Answers 4

15

As of .NET 4.5.2, you can do this using the now-public HeadersWritten property of HttpResponse (see the msdn docs):

if (HttpContext.Current.Response.HeadersWritten) { ... }
Sign up to request clarification or add additional context in comments.

Comments

13

You can use an HttpModule to register for the PreSendRequestHeaders event. When it gets called, write a value to HttpContext.Current.Items indicating that the headers are being sent – and then everywhere else in your code you check the value in HttpContext.Current.Items to see if its been sent yet.

2 Comments

Looks like this might have solved the same problem we are having. Thanks!
If you are able to switch to .NET 4.5.2 you should use HttpResponseBase.AddOnSendingHeaders instead. link
7

UPDATE: the HeadersWritten property is now available on the HttpResponse object.

Unfortunately, whilst the HttpResponse object has a property called HeadersWritten, and a backing field called _headersWritten, neither of these are accessible from outside of the System.Web assembly - unless you use Reflection. I'm not clear what you think you'll be able to obtain from the Headers collection, it may or may not exist, independently of whether the headers have been sent yet.

If you want to use Reflection, it may have it's own performance penalties, and it will require your application to run in full trust.

All of the publicly accessible methods on HttpResponse that involve the _headersWritten field seem to use it to throw an exception.

2 Comments

+1 good observation. The work around I think is the one I suggested.
0

Trying setting buffer to false:

http://msdn.microsoft.com/en-us/library/950xf363.aspx

This will alleviate your first problem but your perfromance and user experience can suffer. Also "This operation requires IIS integrated pipeline mode" is related to non-IIS 7 server processing that line of code. You can find more info on it here:

http://forums.asp.net/p/1253457/2323117.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.