Click here to Skip to main content
Click here to Skip to main content

Compress your ASP.NET 2.0 pages

By , 1 Feb 2006
 

A screenshot of the trace

Introduction

To be on the web with a website that is half successful, you can feel in your wallet pretty quickly. The bandwidth is often a cost factor in the price of a website. Limiting that bandwidth is possible through a few compression standards. I was looking for a way on how to compress my ASP.NET pages when I found the module of Ben Lowery which can be found here. But his implementation never worked properly, not in .NET 1.1 and not in .NET 2.0, at least not in my case.

I downloaded his code and had a read through it, and it was completely .NET 1.1 style and still written like it was using the #ziplib. So, I rewrote his version to something that does work properly and also detects the path properly. In my case, whenever I tried to use his library, the output of my pages was empty.

Using the code

To use the code is pretty easy. Once you've compiled the HttpCompress module, you can add a reference to any .NET 2.0 site. Next, you should set a few parameters in the web.config and you're good to go. If you want to see whether the content is really filtered, you will need something that logs the HTTP pipeline. I use Nikhil Kothari's WebDevHelper BHO to do it. An alternative would be to use Fiddler.

The first thing you can configure are paths and mime-types that are to be excluded from being compressed by the module. I, for one, do not compress any images nor do I compress streaming video etc. because these formats have already been compressed pretty good and you won't be gaining anything significant by compressing them. You are more likely to use more server resources for a small gain.

<configuration>
  <configSections>
    <sectionGroup name="Flanders">
      <section name="HttpCompress" 
        type="Flanders.Library.Modules.HttpCompress.
                 Configuration, HttpCompress"/>
    </sectionGroup>
  </configSections>
  <Flanders>
    <HttpCompress compressionType="GZip">
      <ExcludedPaths>
        <add path="NoCompression.aspx"/>
      </ExcludedPaths>
      <ExcludedMimeTypes>
        <add mime="image/jpeg"/>
      </ExcludedMimeTypes>
    </HttpCompress>
  </Flanders>
  <system.web>
    <httpModules>
      <add name="HttpCompressModule" 
        type="Flanders.Library.Modules.HttpCompress.
                HttpModule,HttpCompress"/>
    </httpModules>
  </system.web>
</configuration>

In the module, you need to hook up an event to the PostReleaseRequeststate. Before, I used to hook it up to the BeginRequest event but this executes way too early for the module to execute. At PostReleaseRequestState, the whole page has executed and its response content has been generated.

   41         public void Init(HttpApplication context)
   42         {
   43             context.PostReleaseRequestState+=
                       new EventHandler(context_PostReleaseRequestState);
   44         }

History

  • 07/01/2006: v.1.0 - Added content to CodeProject and wrote this article.
  • 23/01/2006: v.1.1 - Refactored the module because of problems with JavaScript handling and many more ;-).

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Casual Jim
Software Developer (Senior)
Belgium Belgium
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralFailing in IE6memberaerodave9 Jan '06 - 12:34 
Not sure why.. can't get it to work in IE... seems to take issue with .js files... (client errors "invalid characters")
However, works fine in Firefox.
Neither browser shows "content encoding.. gzip" in the Response headers section of trace, per your above screenshot.
 
Are the .js files even supposed to be processed?
Does the handle wild cards, directory paths, or just file names?
 
It works on the first load but fails on following loads.
 
I appreciate your work and any help on this!
 

 
David

GeneralRe: Failing in IE6 PinmemberCasual Jim10 Jan '06 - 9:03 
Hi David,
 
I checked your comments out and I must admit that there are some problems with the js and css handling.
I've noticed for instance that when you have a freetextbox control on your page it also won't display completely with the handlers at that moment.
 
For now it just handles filepaths on a per file basis. I don't have time during the week to continue and work on this module I will have to wait untill the weekend to add the improved path handling and work out the js issue. (You could try excluding the mime type text/javascript and setting the type in your <script src="..." type="text/javascript" >. Although I haven't had the chance to properly test it all out again.
 
Yep the trace doesn't show anything because I removed the trace lines form the code Wink | ;) That could be an explanation for not showing up in the trace.
 
I hope that this weekend I can find some time to improve this library so it handles everything completely. If anyone makes an updat could you please let me know : koolkraftATgmailDOTcom
GeneralRe: Failing in IE6 PinmemberCasual Jim22 Jan '06 - 16:08 
Hi,
 
I've updated the code which you can download on my blog. It should work now.
The article will be updated shortly as well to have the right code here.
 
in excluded filepaths the ~/ is not supported but it does looks at your website from the root directory. so ~/admin/somefile.aspx becomes admin/somefile.aspx in the web.config
 
http://www.flanders.co.nz/Blog/2006/01/07/CompressingYourASPNET20Pages.aspx[^]
GeneralRe: Failing in IE6 Pinmemberaerodave22 Jan '06 - 19:31 
Glad to see it's moving forward.
 
I'm, however, getting a blank page now. Could be something I'm doing and haven't looked into it too deeply yet.
 
Thanks for your work!
 
David

GeneralRe: Failing in IE6 PinmemberNeetflash23 May '06 - 16:08 
Hello,
 
I'm also experiencing that behavior. Did you find a solution for that?
 
Thank you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 1 Feb 2006
Article Copyright 2006 by Casual Jim
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid