Generating etags for images in asp.net?

2019-07-04 02:30发布

问题:

On my site, users can upload images that are displayed publicly. When the user uploads a new image, it overwrites one of their old images.

Because the images can be large, I want them to be cached. However, when the user uploads a new image, the old cached images should not be used.

The source of the dynamic images looks like this:

userImage.ashx?id=f488b864-0a0b-46d9-af4a-a43cd0dcf751&type=micrositePhoto

So I can inject headers into the response in the .ashx file if I need to.

I've been told to use etags for this purpose, but I don't know how to do this in asp.net. The .js and .css files on my site already have etags, but I don't know what is generating them. Also, any in-built systems to generate etags will probably not understand how my .ashx file works.

How do I generate etags for my site, and make it so that they work as they are supposed to?

回答1:

Looks to me that you should be able to do, for example:

Response.AddHeader("ETag","\"" + Guid.NewGuid().ToString() + "\"");

ETags must be enclosed inside "

NOTE Obviously, once an ETag is returned with an image it needs to be the same on subsequent requests. My code above was just a proof of concept. :)

Edit: Yep, just tested and confirmed using Firebug that the Response headers do contain the Guid that I created.



标签: asp.net etag