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   
General'context_PostReleaseRequestState' does not exist in the current contextmemberrichard.tchicou5 Jan '09 - 0:56 
Hi,
I have tried to use the Http Compression module, I have got the following message: The name 'context_PostReleaseRequestState' does not exist in the current context. Despite the fact, I did add a reference to the assembly!
I will appreciate any help!
 
Cheers,
GeneralNot happymembersmilysanjai6 Oct '08 - 20:51 
Downloaded your code and added dll, I am getting errorors like below:
 
Unrecognized configuration section Flanders.
 

Even the downloaded code is with bugs..
GeneralRe: Not happymemberCasual Jim6 Oct '08 - 22:11 
RTFM
GeneralRe: Not happymembersmilysanjai6 Oct '08 - 22:57 
I disnt get you my dear..
GeneralRe: Not happymemberCasual Jim6 Oct '08 - 23:22 
Read the post above and create your configuration section in your .config file
GeneralRe: Not happymembersmilysanjai7 Oct '08 - 0:17 
Thanks for the replies....Even I did the same...
 

I copied the codes for web.config and I downloaded the code of httpcompress, created a build and added to my project.
 
Still, its saying unrecognized format "Flanders".
 
Please help me in acheiving this GZip.
 
Regards,
Sanjai
GeneralRe: Not happymemberCasual Jim7 Oct '08 - 0:26 
You seem to be missing the following in your web.config if that is the error message.
 
< <section name="HttpCompress"
type="Flanders.Library.Modules.HttpCompress.
Configuration, HttpCompress"/>
</sectionGroup>
 
http://aspnet.4guysfromrolla.com/articles/032807-1.aspx[^]
GeneralRe: Not happymemberCasual Jim6 Oct '08 - 22:12 
RTFM
 
<configuration>
  <configsections>
    <sectiongroup name="Flanders">
      <section name="HttpCompress">
        type="Flanders.Library.Modules.HttpCompress.
                 Configuration, HttpCompress"/>
    </section></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"/>
    </add></httpmodules>
  </system.web>
</configuration>

GeneralRe: Not happymembersmilysanjai7 Oct '08 - 1:56 
I Included that one also.
 
I am getting the below error:
 
Could not load type 'Flanders.Library.Modules.HttpCompress.
HttpModule' from assembly 'HttpCompress'.
GeneralRe: Not happymemberCasual Jim7 Oct '08 - 2:05 
can you run the provided sample project ?
did you compile my code in its own assembly or did you add it to an assembly with a different name?
The code should work because I've put this version in production on some websites that do get a reasonable amount of traffic.
GeneralRe: Not happymembersmilysanjai7 Oct '08 - 2:13 
This is what i did...
 
1. Downloaded your code..
2. Compiled your code and rectified few bugs to make build success
3. I added this reference to my project
4. I copied the config code from the site
 
Now am getting that error. Seems the dll is not proper.
 
Can you mail me the dll's by zip.
 
Thanks in anticipation!
 
My mail id: Prasad_Rambabu@satyam.com
GeneralRe: Not happymemberCasual 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 happymemberCasual Jim7 Oct '08 - 2:48 
I cannot send it to you it seems
 

This is an automatically generated Delivery Status Notification
 
Delivery to the following recipient failed permanently:
 
Prasad_Rambabu@satyam.com
 
Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the recipient domain. We recommend contacting the other email provider for further information about the cause of this error. The error that the other server returned was: 552 552 Default Blocking Message (state 18).
GeneralRe: Not happymembersmilysanjai7 Oct '08 - 22:34 
I am very sorry, I didnt get the mail fro you!
 
Can you please re send it..
 
The downloaded code is with few bugs and I excluded the Default.aspx page from the appn.
 
and then build succeeded.
 
Thanks ...
GeneralRe: Not happymemberCasual Jim7 Oct '08 - 23:10 
Your email always comes back telling me it isn't allowed at your company
you can download the attachment http://flanders.co.nz/wp-content/HttpCompress.zip[^]
GeneralRe: Not happymembersmilysanjai20 Oct '08 - 23:40 
Thanks for the replies my dear.. Finally every thing is working fine.. But the images, stylesheets and Javascripts are not working since the httpcompresser is compressing these files also.
How can I avoid this?
 
Sanjai
GeneralRe: Not happymemberCasual Jim20 Oct '08 - 23:43 






GeneralRe: Not happymembersmilysanjai21 Oct '08 - 3:06 
Can you please respond...!
 
I am getting the below error:
 
Error: The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.

I am excluding the Images, css files in this manner:
 
<ExcludedPaths>
<add path="idsp_login.aspx"/>
<add path="idsp_home.aspx"/>
<add path="App_Themes\Stylesheet.css"/>
<add path="App_Themes\Styles.css"/>
<add path="Images\*.jpg"/>
<add path="web.sitemap"/>
<add path="App_Code\*"/>
</ExcludedPaths>
<ExcludedMimeTypes>
<add mime="App_Themes\Stylesheet.css"/>
<add mime="Images\jpg"/>
</ExcludedMimeTypes>
 
Regards,
Prasad.
GeneralRe: Not happymembermercede14 Oct '11 - 6:15 
you must be an Indian guy. It seems in india people prefer google more than their brain
GeneralRe: Not happymemberSmart_Boy26 Jan '12 - 3:28 
Dude,
 
For Google also u need brain.. which others might lack in it..
Better be careful before commenting on Indians...
:P
Regards,
Smart Boy
Mumbai,
(INDIA)

GeneralAjax Problemmemberreturnofjedi23 Aug '07 - 2:10 
Hi firstly thanks for usefull code, i tried to use your DLL with ajax tool kit, but it creates problem.
 
Sys.Web.Forms.PageRequestManagerParser.ErrorException: The message received from server could not be parsed. Common causes for this error are when the response is modified by cals to Response.Write(), response filters HttpModules or server trace is enabled
Details Error parsing near '/table>|html'
GeneralWebResource.axdmemberecellcell30 Nov '06 - 8:58 
The scripts in WebResource.axd not work when using the Compress module.
I've tried excluding the path WebResource.axd from the compression configuration, but it's no help.
 




Anyway to work around this?
GeneralResponse.WriteSubstitution Does not workmemberGFunny197210 Nov '06 - 7:39 
I love your control but now my Substituion Control and Response.WriteSubstitution do not work - it continues to cache the whole page and the code in aspx is not that the MethodName refers to is not executed.
GeneralRe: Response.WriteSubstitution Does not workmemberCasual Jim10 Nov '06 - 8:20 
I'm sorry but I have completely no idea what you are talking about.
Where does the substitution control come from an where do you get Response.WriteSubstition ?
Is that from the control tookit maybe ?

GeneralRe: Response.WriteSubstitution Does not workmemberGFunny197213 Nov '06 - 4:07 
Its called Post Cache Substituion - http://quickstarts.asp.net/QuickStartv20/aspnet/doc/caching/output.aspx#postcache[^
 
It allows you to cache a page or control but still keep one portion of the page dynamic. When using the HTTPCompress, it doesn't execute the page/control code for the dynamic portion of the page it simply caches the whole page. What I have done for now is excluded the pages that use it for now.
Generalhttpcompress and focus() Eventmembergablersoftware10 May '06 - 2:18 
Hi,
I'm new here and I have a problem with httpcompress. I used it in an ASP 2.0 .NET Project and first it seems to work really fine. But now, after execute an postback event, a Focus()-method doesn't work correctly. So, after leaving a textbox the focus doesn't go to the desired control and the browser signs an error.....sorry for my english and thanks for an answer!
 
Helmut
GeneralRe: httpcompress and focus() EventmemberCasual Jim10 May '06 - 10:30 
Hi Helmut
 
Try adding webresource.axd to the excluded paths that should fix the problem
 

GeneralRe: httpcompress and focus() Eventmembergablersoftware11 May '06 - 21:05 
Thanks very much for your answer,
now everything is o.k.!!!!!!!!!
GeneralCompression While Posting Back...memberalidehghan6 Feb '06 - 21:00 
Hello,
 
I have a question. I knew that we can compress asp.net pages while sending to the client, but I don't know how can compress it agian while posting back to the server from client. any Idea?
 
Thanks in advance...
GeneralRe: Compression While Posting Back...memberCasual Jim7 Feb '06 - 8:13 
you can't.
The compression takes place on the server this means that we can compress everything from there. When you postback it shoould send back the compressed data to the server because it is already compressed when it was received (I haven't tested it though)
 

GeneralCSS & JS Includesmemberjoeyb10003 Feb '06 - 3:30 
I noticed (thru tracing and httplook) that when using the cassini build in webserver with VS2005 that all requests (aspx, css, js) are processed and compressed, but when I run the site on IIS 6.0 it only compresses the aspx files not the css and js. After looking at this I realised that IIS is not configured to pass requests for css and js to the .net pipeline. Is there any way around this or am I missing something.
GeneralRe: CSS & JS IncludesmemberCasual Jim7 Feb '06 - 8:04 
map them in your web.config to the aspx handler





 

QuestionHow I check up that "output stream of an ASP.NET page" is done?memberDolonk24 Jan '06 - 21:17 
subj
AnswerRe: How I check up that "output stream of an ASP.NET page" is done?memberCasual Jim24 Jan '06 - 22:26 
I use the WebDevHelper BHO from nikhil kothari http://www.nikhilk.net
 
Another option would be to use fiddler
QuestionMedium trust?memberaerodave22 Jan '06 - 19:33 
Any chance of making it work in Medium trust config?
 
Thanks!
 
David

GeneralJavascript nastiesmembervaticnz11 Jan '06 - 9:15 
Thanks for the cool article and code!
 
I too am having problems with javascript errors (specifically the framework 'WebForm' modules).
Have tried excluding text/javascript but doesn't help.
 
Shame. Here's hoping you get a chance to resolve this Smile | :)
 
Cheers
Rich
GeneralRe: Javascript nastiesmemberCasual Jim22 Jan '06 - 16:05 
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.
 
http://www.flanders.co.nz/Blog/2006/01/07/CompressingYourASPNET20Pages.aspx[^]
GeneralRe: Javascript nastiesmemberahoja7 Feb '06 - 22:48 
But for me, it's still not working. I had to manually exclude WebResource.axd, ie. .

QuestionWhat kind of files should be excluded?memberMorningZ10 Jan '06 - 6:04 
I see the excluded node in the web.config and was wondering why anything should not be gzip-ed ?
 
============================
If you make it idiot-proof, they'll make a better idiot
AnswerRe: What kind of files should be excluded?memberCasual Jim10 Jan '06 - 8:49 
I can think of several filetypes that you'd want to exclude.
All multimedia files that are : images, videofiles, and flash movies....
 
The reason for this would be that most of these mimes are already in a highly compressed format and they will slow your site down when zipped, while not getting any considerable gain in bandwith preservation.
 

 

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 IE6memberCasual 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 IE6memberCasual 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 IE6memberaerodave22 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 IE6memberNeetflash23 May '06 - 16:08 
Hello,
 
I'm also experiencing that behavior. Did you find a solution for that?
 
Thank you.
GeneralIIS CompressionmemberJon Sagara6 Jan '06 - 13:46 
If you have access to your IIS 6.0 server, you can enable IIS compression.
 
IIS Compression in IIS6.0[^]
 
Jon Sagara
Look at him. He runs like a Welshman. Doesn't he run like a Welshman? Doesn't he? I think he runs like a Welshman.
Sagara.org | Blog | My Articles
GeneralRe: IIS CompressionmemberCasual Jim6 Jan '06 - 13:58 
Smile | :) I know but with this module you can do it with or without support of IIS. Although I think that IIS compression will offer a more configurable option in means of quality etc.Smile | :)
GeneralRe: IIS CompressionmemberJoshua Lunsford8 Jan '06 - 11:39 
this way you can host on noncontroled servers and get compression even if the host doesnt want you too(to get more money out of you)

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

Permalink | Advertise | Privacy | Mobile
Web02 | 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