Compressing files before they are sent to the browser has many benefits. The page will load faster and less bandwidth is used, resulting in better user experience and reduced cost. There are a few different HTTP Compression module implementations out there, but I wanted one that would compress WebResource.axd. I couldn't find any that did, so I tried to find a way to do it myself.
HttpModule will replace the link with an appropriate js.axd?files=jsfiletocompress or css.axd?files=csstocompress.css. So the page will work with or without the compression enabled. This makes it a lot easier and allows us to take advantage of Visual Studio 2008's designer.
There are a couple of changes that I made to the original Flanders
I might mention that if you are running a page from Visual Studio 2005's development server, all mime types will go through the compression module, but if you run it from IIS, they will not. Images, JS, and CSS files, and static pages do not go through the ASP.NET pipeline but are served up directly.
I also added code to make it work with update panels. This is done by adding...
if (app.Request["HTTP_X_MICROSOFTAJAX"] != null)
... to the beginning of the
I do not claim to be a C# guru, and as such, there may be better ways to do what I am doing. I welcome any comments or suggestions to make this code better. This code should be considered beta software. Also, there are many improvements and features that may be added, but I don't have time to do so. I have been successful in using this code at the University where I work, which uses load balanced servers running IIS6, and on my personal Website running in a medium trust environment (hosted by GoDaddy). I cannot, however, guarantee that it will work for you.
This project was written using Visual Studio 2005 and C# 2.0 and is not meant to run in a .NET 1.1 environment.
Using the Code
Add a reference to the DLL or drag and drop the DLL into the bin folder of the Web application. Add the following to the web.config. That's it! It should work transparently, meaning you can develop your site without the compression module, then add the compression later; or develop with the compression module and then remove it later without having to make any changes besides deleting the web.config items and the DLL.
The only settings that have been added is the use of the
IncludedPaths has not been fully tested),
<IncludedMimeTypes> is described above and tells the module what mime types to compress. Again, you may use '*' to compress all mime types that may pass through the module.
<section name="HttpCompress" type="DC.Web.HttpCompress.Configuration,
<add mime="text/html" />
<add mime="text/html" />
<add path="~/NoCompress/Default.aspx" />
<add verb="*" path="js.axd,css.axd"
<!--<span class="code-comment"> The compression module must be enabled for the WebResource.axd to be
Caching the Files
The files are cached under the
context.Cache on the server and on the browser using Etags.
- 03/18/2008 - Article, source code and demo updated
HttpHandler. Changed the
HttpHandler to cache differently on the server and to compress the output differently.
- 05/17/2007 - Updated code which caused CSS errors
- 04/30/2007 - Created article