1

Hi,

When a webpage with a image(img_01.jpg) is loaded the image will be cached by the browser. Say that we update img_01.jpg and saves it with the same name, when visiting the same page the old image will be shown from the cache.

How should I handle this in ASP.NET MVC?

I know that in regular ASP.NET(no MVC) a extra component had to be created and this component would add a random number at the end of the filename. How do I do this in ASP.NET MVC?

BestRegards

Banshee
  • 15,376
  • 38
  • 128
  • 219

3 Answers3

1

You can use HTTP handlers for images to add expiration headers. But it mostly depends on a browser if expiration headers are used correctly. Does it help?

  • hmm, looked at your link and I do remember doing somthing like this a long time ago. What I did was to add a random number at the end like this : myimage.jpg?id=[random] , this made sure that the image was loaded everytime from the server. I did however never needed to change anything on the IIS as this article sugests. The question is if this is really better then adding a random string to the end of the real filename? This last solution would mean that we do only download new image when its got a new random number(is updated). – Banshee Feb 13 '12 at 20:36
  • Adding a random string makes an image to be a different resource from browser's point of view. And it's OK if you can live with such images' names (myimage.jpg?id=474484). In some cases it may be not good. So, then you can handle browser's request to images and add an expiration header (with date set to past) to the response. It will tell browser that resource has already expired and the next time it will request it again from the web server. – Vladimir Mischenko Feb 13 '12 at 20:45
0

In C# when u r binding Image control just make the url as query string. Ex:-

string  _urlPhoto = "UploadPhoto" + "/" + Imagename + "?" + Guid.NewGuid().ToString();

Browser will always see it as new request and it will not take image from CACHE.

sgarizvi
  • 16,623
  • 9
  • 64
  • 98
  • This is a very poor solution. The whole idea of caching is to improve performance. This solution throws away the whole performance optimization attempt. – CodingYoshi Apr 22 '18 at 20:28
0

I decided to use the main object version number in the image filename, this will make sure that the end user always sees the correct image.

Banshee
  • 15,376
  • 38
  • 128
  • 219