Click here to Skip to main content
15,879,326 members
Articles / Web Development / ASP.NET
Article

Compress your ASP.NET 2.0 pages

Rate me:
Please Sign up or sign in to vote.
4.11/5 (3 votes)
1 Feb 2006MIT2 min read 165.2K   1.5K   71   48
An article on how to zip the output stream of an ASP.NET page.

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.

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

C#
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


Written By
Software Developer (Senior)
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General'context_PostReleaseRequestState' does not exist in the current context Pin
richard.tchicou5-Jan-09 0:56
richard.tchicou5-Jan-09 0:56 
GeneralNot happy Pin
smilysanjai6-Oct-08 20:51
smilysanjai6-Oct-08 20:51 
GeneralRe: Not happy Pin
Casual Jim6-Oct-08 22:11
Casual Jim6-Oct-08 22:11 
GeneralRe: Not happy Pin
smilysanjai6-Oct-08 22:57
smilysanjai6-Oct-08 22:57 
GeneralRe: Not happy Pin
Casual Jim6-Oct-08 23:22
Casual Jim6-Oct-08 23:22 
GeneralRe: Not happy Pin
smilysanjai7-Oct-08 0:17
smilysanjai7-Oct-08 0:17 
GeneralRe: Not happy Pin
Casual Jim7-Oct-08 0:26
Casual Jim7-Oct-08 0:26 
GeneralRe: Not happy Pin
Casual Jim6-Oct-08 22:12
Casual Jim6-Oct-08 22:12 
GeneralRe: Not happy Pin
smilysanjai7-Oct-08 1:56
smilysanjai7-Oct-08 1:56 
GeneralRe: Not happy Pin
Casual Jim7-Oct-08 2:05
Casual Jim7-Oct-08 2:05 
GeneralRe: Not happy Pin
smilysanjai7-Oct-08 2:13
smilysanjai7-Oct-08 2:13 
GeneralRe: Not happy Pin
Casual Jim7-Oct-08 2:47
Casual Jim7-Oct-08 2:47 
I've sent it
For me they compiled without warnings or errors and without having to change the code.
GeneralRe: Not happy Pin
Casual Jim7-Oct-08 2:48
Casual Jim7-Oct-08 2:48 
GeneralRe: Not happy Pin
smilysanjai7-Oct-08 22:34
smilysanjai7-Oct-08 22:34 
GeneralRe: Not happy Pin
Casual Jim7-Oct-08 23:10
Casual Jim7-Oct-08 23:10 
GeneralRe: Not happy Pin
smilysanjai20-Oct-08 23:40
smilysanjai20-Oct-08 23:40 
GeneralRe: Not happy Pin
Casual Jim20-Oct-08 23:43
Casual Jim20-Oct-08 23:43 
GeneralRe: Not happy Pin
smilysanjai21-Oct-08 3:06
smilysanjai21-Oct-08 3:06 
GeneralRe: Not happy Pin
Mercede14-Oct-11 6:15
Mercede14-Oct-11 6:15 
GeneralRe: Not happy Pin
Smart_Boy26-Jan-12 3:28
Smart_Boy26-Jan-12 3:28 
GeneralAjax Problem Pin
returnofjedi23-Aug-07 2:10
returnofjedi23-Aug-07 2:10 
GeneralWebResource.axd Pin
ecellcell30-Nov-06 8:58
ecellcell30-Nov-06 8:58 
GeneralResponse.WriteSubstitution Does not work Pin
GFunny197210-Nov-06 7:39
GFunny197210-Nov-06 7:39 
GeneralRe: Response.WriteSubstitution Does not work Pin
Casual Jim10-Nov-06 8:20
Casual Jim10-Nov-06 8:20 
GeneralRe: Response.WriteSubstitution Does not work Pin
GFunny197213-Nov-06 4:07
GFunny197213-Nov-06 4:07 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.