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();

  }

The new System.IO.Compression namespace in .NET 2.0 makes it easy to implement HTTP compression without having to touch IIS. The best thing about it is that you no longer need any third party compression components, it’s all build directly into .NET Framework.

There are different ways to implement the compression but I think an HttpModule is the right choice for this feature. Let's create one and call it CompressionModule.

The CompressionModule must adhere to the following rules:

  • Support both GZip and Deflate compression
  • Only compress if the browser supports it
  • Simplest possible implementation

These rules are important to make sure that the compression will run smoothly in every situation.

The code

An HttpModule is a class that implements the IHttpModule interface and gives it direct access to the underlying HttpApplication. We have to implement the Init method and attach the Application.BeginRequest event and the event handler that will do the compression.

#region Using   using System; using System.Web; using System.IO.Compression;   #endregion   /// <summary> /// Compresses the output using standard gzip/deflate. /// </summary> public class CompressionModule : IHttpModule {     #region IHttpModule Members     void IHttpModule.Dispose()   {     // Nothing to dispose;   }     void IHttpModule.Init(HttpApplication context)   {     context.BeginRequest += new EventHandler(context_BeginRequest);   }     #endregion     #region Compression     private const string GZIP = "gzip";   private const string DEFLATE = "deflate";     void context_BeginRequest(object sender, EventArgs e)   {     HttpApplication app = sender as HttpApplication;     if (app.Request.RawUrl.Contains(".aspx"))     {       if (IsEncodingAccepted(GZIP))       {         app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);         SetEncoding(GZIP);       }       else if (IsEncodingAccepted(DEFLATE))       {         app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);         SetEncoding(DEFLATE);       }     }

>

  }     /// <summary>   /// Checks the request headers to see if the specified   /// encoding is accepted by the client.   /// </summary>   private bool IsEncodingAccepted(string encoding)   {     return HttpContext.Current.Request.Headers["Accept-encoding"] != null && HttpContext.Current.Request.Headers["Accept-encoding"].Contains(encoding);   }     /// <summary>   /// Adds the specified encoding to the response headers.   /// </summary>   /// <param name="encoding"></param>   private void SetEncoding(string encoding)   {     HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);   }     #endregion  

}

Implementation

Download the CompressionModule.cs file below and add it to the App_Code folder in the root of your website. Then add these lines to the web.config’s <system.web> section.

 <httpModules>    <add type="CompressionModule" name="CompressionModule" />  </httpModules>

That’s all you have to do to enable HTTP compression on an ASP.NET 2.0 website.

Download

 

CompressionModule.zip (0,75 KB)