4

How can I completely supress the output of the Cache-Control Header that is returned by my custom HttpHandler in ASP.NET?

I know, I can change the header field by modifying response.Cache and response.Cache.SetCacheability, but that will only change the header field, not remove it. That is not what I want. I completely want to make no assumptions about the Cache-Control field and leave it up to the browsers policy.

EDIT: The same holds true for HttpResponse.Charset. If no charset is set, ASP.NET will always set it to "utf-8", although returning a header "Content-Type: text/html" without the charset parameter is perfectly valid html. Any idea how to supress the charset, too?

1
  • The provided answers target ASP.NET on IIS which might work, but is untested for me since I use a mono/fastcgi/nginx stack. I now use the nginx configuration directive fastcgi_hide_header to supress the Cache-Control in the response, which works perfectly for me. Same would work on IIS if you use nginx as a HTTP reverse proxy with the proxy_hide_header directive. Commented Sep 10, 2012 at 5:48

2 Answers 2

1
+50

You can remove header values using

HttpContext.Current.Response.Headers.Remove("HEADER-VALUE-HERE"); 

If you are using a IIS7 integrated pipeline, however some header values are protected and cannot be removed (i.e they are injected after the response is dealt with on the most common page lifecycles).

What you can do if the above fails (ie its protected) is you can make a module to manually hook into the PreSendRequestHeaders and use the code above to remove the header at this point.

The majority of problems removing the header is not the inabillity to remove it, its to remove it at the correct time.

I believe using a custom http handler you should be able to remove it at the instantiation of the handler though.

Sign up to request clarification or add additional context in comments.

1 Comment

Headers.Remove as well as the PreSendRequestHeaders do not work on the ASP.NET Development server, VS2010 complains that you have to use IIS. Since I am going for a nginx/mono/fastcgi approach, this would'nt help me, though I didn't test it. However I will mark your question as answered for the bountys sake, as it MIGHT work an IIS systems (but untested from me). Sadly, neither work on current mono instances.
0

I'm only guessing here, but I would think that this is configured within IIS -- on the website (or virtual directory)'s Properties dialog, there is a section called "Enable content expiration". Does unticking this box suppress the header?

If you don't have access to your IIS Manager, then I'm afraid I am not sure how you could suppress that header. Looking at the docs, response.Headers is a read-only property, so setting it to an empty string isn't going to work...

3 Comments

Well, I can't tell as I use mono so no IIS is involved. But (guessing here too) content expiration only modifies the Expire: header field, not Cache-Control (which are 2 completely different headers and do not depend on each other)
Ah, I'm afraid I know nothing about Mono, sorry. Yes, I know the two headers are different, but I think that the same IIS setting controls both. Like I say, though, I am just guessing.
I think IIS options are totally unrelated. I've found out (see my edit on the charset= parameter) that some headers are always set, even though I do .ClearHeaders() and set my own. I think ASP.NET enforces these headers giving me no control over them. But of course, I will check with IIS to rule out any mono bugs.

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.