Geeks With Blogs

Lance's TextBox
You can automatically serve all pages in your .Net web apps using HTTP compression by creating a simple HTTPModule and referencing it in your web.config.  This particular implementation uses IPWorksZip ZipStream component.

To create the module, just inherit from IHttpModule. In the IHttpModule.Init function, add an event handler for context.BeginRequest. Inside this event, check the Request object to see if encoding is accepted, and if so - use the Zipstream component to create a compression stream to pass to Response.Filter. For example:

if (HttpContext.Current.Request.Headers["Accept-Encoding"].ToLower().IndexOf("deflate") > 0)
 nsoftware.IPWorksZip.Zipstream z = new nsoftware.IPWorksZip.Zipstream();
 z.StreamFormat = nsoftware.IPWorksZip.ZipstreamStreamFormats.sfDeflate;
 HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
 HttpContext.Current.Response.Filter = z.GetCompressionStream(HttpContext.Current.Response.Filter);

If the Accept-Encoding header includes "deflate" as one if its values, the client supports deflate compression. Gzip or Zlib could also be used (ZipStream supports them both) if the client specifies them in the Accept-Encoding header.

Below is a samle web.config file that you can use to incorporate this compression module. Use the text below for the entire config file, or copy just the httpModules section into an existing web.config file.

xml version="1.0"?>
    <authentication mode="None"/>
      <add name="HttpCompressionModule" type="ipzaspx.cs.HttpCompressionModule, ipzaspx"/>
Posted on Friday, July 22, 2005 12:18 PM Programming | Back to top

Comments on this post: Automatically serve all pages in your .Net web apps using HTTP compression

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Lance Robinson | Powered by: