views:

463

answers:

3

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.

A: 

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

mfeingold
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.
patmortech
+2  A: 

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.

RickNZ
Using IIS 5.1 on WinXP Pro, and IIS 6 on my hosting site.
patmortech
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.
RickNZ
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).
patmortech
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.
RickNZ
That's it!! Can you put that into an answer so that I can mark it appropriately?
patmortech
Sure, I edited my answer.
RickNZ
A: 

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.

CodeTwice