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

 

CompressionModule.zip (0,75 KB)

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:

  1. Download the Binary DLL.
  2. 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
  3. Modify your Web.config
    1. Add to the configSections
      <sectionGroup name="blowery.web">
       <section name="httpCompress" type="blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress"/>
      </sectionGroup>
    2. 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>
    3. 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>

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%.

Tagged Under ( )

12 Responses to “HTTP Compression in ASP.NET 2.0

  • 1
    Chris
    March 3rd, 2007 01:59

    I tried this and it worked, but it broke the .net Menu object. Each page displayed a java error of Object not found. Any idea how to solve this?

  • 2
    PohEe
    March 3rd, 2007 07:33

    I got no idea on failure of the .Net Menu object. I am very much happy to troubleshoot your problem if you send me your code.

  • 3
    Chad
    March 24th, 2007 03:16

    I have figured out a way to get around the validation issue using blowery. Exclude ‘WebResource.axd’ as a path and is should work.

  • 4
    Chad
    March 24th, 2007 04:51

    Strike that, it allows it to work in Firefox but IE still whines.

  • 5
    yankee
    April 1st, 2007 10:22

    thanks for your excellent work. i tried I the Mads Kristensen’s way at first, but got the damn problem with my rss output page(http://knay.net/ylog/rss.aspx).

    then i came here via google. i tried your solution based on Blowery. till now,it works fine!

    thanks again~

  • 6
    Maven
    June 21st, 2007 11:40

    No luck when i am trying it with ASP.NET 2.0 AJAX.

  • 7
    Daniel Nøhr
    June 25th, 2007 01:51

    Thanks for your article, great to read about this compression.

    But I did also find the javascript problem automatic generated from asp.net, I did recompile the solution and excluded the “WebResource.axd” file, but with no luck, my treeview did still make errors, so I did try excluce ScriptResource, and that did resolve my problem, so if some of you will get this error, try exclude the automatic asp.net file, ScriptResource.axd.

    Im currently using this compression on my personal blog, dnohr.dk, and my flash game portal projek Grinern.dk

  • 8
    Miron
    September 22nd, 2007 08:07

    WebResource.axd files are not small, and compress them (and save in the cache) will really improve site performance.
    I posted in my blog complete module to compress pages,
    js,cs and WebResource.axd file.

  • 9
    Harsh Trivedi
    October 4th, 2007 19:57

    Hi,
    I have implemented the new dlls for the .net 2.0 from http://www.blowery.org/code/httpcompress_6_net2_binary.zip.

    My treeview did makes the error and it is not working correctly.

    except this, it is working really good.

    Can any one help me out, why the tree view is not working when i am adding these dlls to my project?

  • 10
    Harsh Trivedi
    October 4th, 2007 20:14

    This Works………………………!!!!!!!!!!!!!

  • 11
    Harsh Trivedi
    October 4th, 2007 20:15

    blowery.web
    httpCompress preferredAlgorithm=”deflate” compressionLevel=”high”
    excludedMimeTypes
    add type=”image/jpeg”
    add type=”image/gif”
    excludedMimeTypes
    excludedPaths
    add path=”WebResource.axd”
    add path=”ebResource.axd”
    add path=”ScriptResource.axd”
    add path=”NoCompress.aspx”
    excludedPaths
    httpCompress
    blowery.web

  • 12
    Debbie F
    November 10th, 2007 02:39

    Ok, I must be missing something still. I’ve got the new dlls (made the change to source code in httpcompress.cs as described), added all the appropriate sections to web.config but still get a blank page when compression is turned on.

    Here’s my web.config definitions:
    <sectionGroup name=”blowery.web”>
    <section name=”httpCompress” type=”blowery.Web.HttpCompress.SectionHandler, blowery.Web.HttpCompress”/>
    </sectionGroup>

    <httpModules>
    <add name=”CompressionModule” type=”blowery.Web.HttpCompress.HttpModule, blowery.web.HttpCompress”/>
    </httpModules>

    <blowery.web>
    <httpCompress preferredAlgorithm=”deflate” compressionLevel=”high”>
    <excludedMimeTypes>
    <add type=”image/png” />
    <add type=”image/jpeg” />
    <add type=”image/gif” />
    <add type=”application/zip” />
    <add type=”application/x-zip-compressed” />
    <add type=”application/x-gzip-compressed” />
    <add type=”application/x-compressed” />
    <add type=”application/octet-stream” />
    <add type=”application/pdf” />
    </excludedMimeTypes>
    <excludedPaths>
    <add path=”WebResource.axd”/>
    <add path=”ScriptResource.axd”/>
    </excludedPaths>
    </httpCompress>
    </blowery.web>

    Can anyone help me figure out is configured incorrectly? I’m migrating a 1.1 project to 2.0 where the older version of blowery worked great. I just can’t get it working in 2.0 version.

Leave a Reply