Search code examples
iiscachingashx

How to prevent IIS from sending cache headers with ASHX files


My company uses ASHX files to serve some dynamic images. Being it that the content type is image/jpeg, IIS sends headers with them as would be appropriate for static images.

Depending on settings (I don't know all of the settings involved, hence the question) the headers may be any of:

LastModified, ETag, Expires

Causing the browser to treat them as cacheable, which leads to all sorts of bugs with the user seeing stale images.

Is there a setting that I can set somewhere that will cause ASHX files to behave the same way as other dynamic pages, like ASPX files? Short of that, is there a setting that will allow me to, across the board, remove LastModified, Etag, Expires, etc and add a no-cache header instead?


Solution

  • Only solutions I've found were:

    1) Adding Response.ContentControl = "no-cache" to each handler.

    I don't like this because this requires all of the handlers to change and for all developers to be aware of it.

    2) Setting HTTP Header override on a folder where the handlers live

    I don't like this one because it requires the handlers to be in their own directory. While this may be good practice in general, unfortunately our application is not structured that way, and I cannot just move them because it would break client-facing links.

    If nobody provides a better answer I'll have to accept that these are the only two choices.