HTTP Compression in ASP.NET 2.0
These two days, I was looking for a way to enable HTTP Compression in ASP.NET 2.0 for my project - AquariumFishExporter.com.
Last time, I tried to enable to http compression in IIS6 for ASP.NET 1.1. Here is a good guide to follow - IIS Compression in IIS6.0. Currently, I am using a shared web hosting with no access to the server configuration. So, I have to find a way to enable the compression without touching the IIS settings.
At first I tried the Mads Kristensen’s way of compression.
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.
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
Unfortunately, the compression is activated but all the javascript validation doesn’t work as expected. It caused by the compression on the WebResource.axd.
The .NET 2.0 framework changed the way clientside JavaScript is delivered to the browser. Previously, ASP.NET 1.1 used the aspnet_client directory whereas now 2.0 uses WebResource.axd.
The only solution to solve this problem is exclude the compression on WebResource.axd. But, I couldn’t find a way to do it.
Then, I tried a another famous Http Compression engine for ASP.NET 2.0 - Blowery.
Blowery - This is a pretty simple recompile of the version 6 source, targetting the 2.0 version of the .NET Framework. It now uses the built-in deflate and gzip streams found in System.IO.Compression instead of #ziplib. I consider this a beta release; please test it thoroughly before releasing it onto any production systems. [Binary Only, Source Only]
Here is the implementation plan:
- Download the Binary DLL.
- Extract the package and copy the extracted files into the bin folder of your .Net application.
blowery.Web.HttpCompress.dll
blowery.Web.HttpCompress.dll.xml
ICSharpCode.SharpZipLib.dll
ICSharpCode.SharpZipLib.dll.xml
- Modify your Web.config
-
- Add to the configSections
<sectionGroup name="blowery.web">
<section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress"/>
</sectionGroup> - Add to the httpModules section inside system.web (if httpModules section does not exist, create it)
<httpModules>
<add name="CompressionModule" type="blowery.Web.HttpCompress.HttpModule, blowery.web.HttpCompress"/>
</ httpModules> - Add to the configuration section
<blowery.web>
<httpCompress preferredAlgorithm="deflate" compressionLevel="high">
<excludedMimeTypes>
<add type="image/jpeg"/>
<add type="image/gif"/>
</excludedMimeTypes>
<excludedPaths></excludedPaths>
</httpCompress>
</blowery.web>
- Add to the configSections
However, it still giving the same problem on the javascript validation. Luckily, I managed to find Ross Hawkins’s Troubleshooting WebResource.axd. He suggested to download the source code from Browely and change a small code in Line 85, httpCompress.cs:
From
string realPath = app.Request.Path.Remove(0, app.Request.ApplicationPath.Length+1);
To
string realPath = Path.GetFileName(app.Request.Path);
Then, I recompile the library and add it into my project. Finally, it works.
Here is the statictic of enabling http compression:
Thanks to Port80 - Http compression Checker
Now, my page load 4.5 times faster. The compression rate is 79%.
