IIS 7 Force Fresh Images

How to force IIS 7 to non- cache images for a specific page?

+4
source share
3 answers

In IIS7, you can do this declaratively in your web.config or programmatically.

<location path="YourPath"> <system.webServer> <staticContent> <clientCache cacheControlMode="DisableCache" /> </staticContent> </system.webServer> </location> 

The software solution requires a simple HttpModule, which is registered to run for all requests in integrated mode, where you look for URLs that bother you. Then call:

 context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 

FWIW, you may want to disable client-side caching only while caching on the server side using HttpCacheability.ServerAndNoCache . In addition, if you add a query string to image names, you will prevent http.sys server-side caching.

In case this helps, I will talk in detail about such methods in my book: Ultra-fast ASP.NET .

+5
source

I would think your browser is caching.

In any case, one way around this, until your link is statically declared in html, is to add a random number to the end of the image url:

 <img src="http://mywebsite/images/mypic.png?a=123456" /> 

the argument means nothing because you are not doing anything with it, but in the browser it looks like a new uncached link.

How you put this random number at the end is up to you:

 <img src="javascript:getMyLink();" /> 

or from the code behind:

 Image myImage = new Image(); myImage.Source = "myurl?a=" + Guid.NewGuid().ToString(); someOtherControl.Controls.Add(myImage); 

(of course, this is pseudo code, you need to check that the property names are correct).

+4
source

I had to deal with this a lot, but I need to better understand the final goal, since IIS7 will update its cache if the image is changed on the server, so maybe what you see is the browser cache, have you looked to ethics?

An old fall back is to insert a random query string at the end of the image path, which makes the browser guess.

One surefire way to prevent it from caching is to create your own file handler for .gif, .jpg, .png extention (lookup iHttpHandler) (the code below is from http://www.codeguru.com/csharp/csharp/cs_network/http/article. php / c12641 /

 using System.IO; using System.Web; using System.Globalization; namespace MVPHacks { public class ImageHandler: IHttpHandler { public void ProcessRequest(System.Web.HttpContext ctx) { HttpRequest req = ctx.Request; string path = req.PhysicalPath; string extension = null; string contentType = null; extension = Path.GetExtension(path).ToLower(); switch (extension) { case ".gif": contentType = "image/gif"; break; case ".jpg": contentType = "image/jpeg"; break; case ".png": contentType = "image/png"; break; default: throw new NotSupportedException("Unrecognized image type."); } if (!File.Exists (path)) { ctx.Response.Status = "Image not found"; ctx.Response.StatusCode = 404; } else { ctx.Response.StatusCode = 200; ctx.Response.ContentType = contentType; ctx.Response.WriteFile (path); } } public bool IsReusable { get {return true; } } } } 

And don't forget to remove the default image handlers and add them to both sections of web.config

 <httpHandlers> <clear /> <add verb="*" path="*.jpg" type="MVPHacks.ImageHandler" /> <add verb="*" path="*.gif" type="MVPHacks.ImageHandler" /> <add verb="*" path="*.png" type="MVPHacks.ImageHandler" /> </httpHandlers> <handlers> <clear /> <add verb="*" path="*.png" type="MVPHacks.ImageHandler" name="png" /> <add verb="*" path="*.gif" type="MVPHacks.ImageHandler" name="gif" /> <add verb="*" path="*.jpg" type="MVPHacks.ImageHandler" name="jpg /> </handlers> 
+1
source

All Articles