2

I have an HttpHandler (have also done this as an ASPX page) that retrieves an image stored in the db and writes it out to the response. I have added the following lines to the code to try and get the images to cache in the browser, but whenever I look at the response in Firebug, it always has a cache-control header value of "private".

Response.Cache.SetCacheability(HttpCacheability.Public)

I've tried all sorts of things, like using the Response.ClearHeaders & Response.AddHeader() to manually add the "Cache-Control" header value, but to no avail. Any ideas?

Edit:
More info: This is running in an HTTP Handler (.ashx), and I have tested it both on my local IIS 5.1, and on the hosting site which I think is IIS 6.

4 Answers 4

6

Does the page by any chance require authentication? The runtime will force Cache-Control: private on pages that require authentication to prevent the accidental caching of private content on public proxies.

Are you using Cassini? If so, it always forces Cache-Control: private. If so, you might try switching to IIS instead.

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

5 Comments

Using IIS 5.1 on WinXP Pro, and IIS 6 on my hosting site.
So you have Visual Studio configured to use IIS on your development machine, rather than using the built-in web server? If so, IIS 5 may not support this method; the web server that's integrated with Visual Studio definitely does not.
AS I mentioned in my comment, I tried this same handler on my hosted site, which is most likely on IIS 6, and got the same response header (Cache-Control: private).
Does the page by any chance require authentication? The runtime will force Cache-Control: private on pages that require authentication to prevent the accidental caching of private content on public proxies.
That's it!! Can you put that into an answer so that I can mark it appropriately?
1

Just found this response, it helped me out but there was one more thing that was preventing the Cache-Control: Public on my setup: if you use multiple Response.Cache.SetCacheability() like:

cache.SetCacheability(HttpCacheability.ServerAndPrivate);
...
cache.SetCacheability(HttpCacheability.Public);

calls, it is the first one winning, so subsequent calls to SetCacheability() do not overwrite the previous setting.

Comments

1

Don't remove the authentication element in the web.config. Instead, Set mode to None. Without authentication tag, the cache-control will always be 'private' even calling setcacheability.

Comments

0

Where do you set this property? If you set it after the first line of the response body is output it will be ignored. For the page itself it means you have to set this flag no later than PreRender

1 Comment

This is in an HTTP Handler during the ProcessRequest() method, so there is nothing output at that point. I even do a Response.Clear() right before setting it.

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.