ASP.NET Cache.SetCacheability(HttpCacheability.Public) not setting header
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.ClearHe开发者_JAVA技巧aders & 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.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.
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.
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.
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
精彩评论