Click here to Skip to main content
6,595,854 members and growing! (22,139 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The MIT License

Compress your ASP.NET 2.0 pages

By Casual Jim

An article on how to zip the output stream of an ASP.NET page.
C#, XML, Windows, .NET 2.0, ASP.NET, WebForms, VS2005, Dev
Posted:6 Jan 2006
Updated:1 Feb 2006
Views:70,010
Bookmarked:62 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 1.96 Rating: 4.11 out of 5

1

2
1 vote, 33.3%
3

4
2 votes, 66.7%
5

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


Member

Occupation: Software Developer (Senior)
Location: Belgium Belgium

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 46 (Total in Forum: 46) (Refresh)FirstPrevNext
General'context_PostReleaseRequestState' does not exist in the current context Pinmemberrichard.tchicou1:56 5 Jan '09  
GeneralNot happy Pinmembersmilysanjai21:51 6 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim23:11 6 Oct '08  
GeneralRe: Not happy Pinmembersmilysanjai23:57 6 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim0:22 7 Oct '08  
GeneralRe: Not happy Pinmembersmilysanjai1:17 7 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim1:26 7 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim23:12 6 Oct '08  
GeneralRe: Not happy Pinmembersmilysanjai2:56 7 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim3:05 7 Oct '08  
GeneralRe: Not happy Pinmembersmilysanjai3:13 7 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim3:47 7 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim3:48 7 Oct '08  
GeneralRe: Not happy Pinmembersmilysanjai23:34 7 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim0:10 8 Oct '08  
GeneralRe: Not happy Pinmembersmilysanjai0:40 21 Oct '08  
GeneralRe: Not happy PinmemberCasual Jim0:43 21 Oct '08  
GeneralRe: Not happy Pinmembersmilysanjai4:06 21 Oct '08  
GeneralAjax Problem Pinmemberreturnofjedi3:10 23 Aug '07  
GeneralWebResource.axd Pinmemberecellcell9:58 30 Nov '06  
GeneralResponse.WriteSubstitution Does not work PinmemberGFunny19728:39 10 Nov '06  
GeneralRe: Response.WriteSubstitution Does not work PinmemberCasual Jim9:20 10 Nov '06  
GeneralRe: Response.WriteSubstitution Does not work PinmemberGFunny19725:07 13 Nov '06  
Generalhttpcompress and focus() Event Pinmembergablersoftware3:18 10 May '06  
GeneralRe: httpcompress and focus() Event PinmemberCasual Jim11:30 10 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Feb 2006
Editor: Smitha Vijayan
Copyright 2006 by Casual Jim
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project