Generating etags for images in asp.net?

家住魔仙堡 提交于 2019-12-10 19:33:39

问题


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.



来源:https://stackoverflow.com/questions/7900986/generating-etags-for-images-in-asp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!