Internet Explorer and the Gecko based browsers (Firefox, Netscape and Mozilla) have different ways of managing their internal cache. The speed of the browser cache is much faster that a server caching, so be sure to do your client-side caching cross browser friendly.

The Gecko based browsers uses the ETag header and Internet Explorer uses the Last-Modified header, so you have to set both of them to make it work. Here’s a simple method that does just that.

  private void SetCachingHeaders(DateTime lastModified)

  {

    Response.Cache.SetLastModified(lastModified);

    Response.Cache.SetETag(lastModified.ToString());

  }

If you serve an image or some other file from ASP.NET, you can make the browser cache dependant of the file you are serving. That means that whenever the file changes, so will the client- and server cache and it all happens automatically.

  private void SetCachingHeaders(string fileName)

  {

    context.Response.AddFileDependency(fileName);

    context.Response.Cache.SetETagFromFileDependencies();

    context.Response.Cache.SetLastModifiedFromFileDependencies();

  }

Comments


Comments are closed