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

Compress Aspx with GZip

By , 7 May 2009
 

Introduction

This is a simple function to compress your aspx page with GZip.

Background

If you need advanced knowledge about compression, please visit this link.

Using the Code

Just call this function in the page render: 

protected override void Render(HtmlTextWriter writer)
{
doCompression();
base.Render(writer); 
}

The method:

public static void doCompression()
{
      HttpContext context = HttpContext.Current;
      HttpRequest request = context.Request;
      string acceptEncoding = request.Headers["Accept-Encoding"];
      HttpResponse response = context.Response;
      if (!string.IsNullOrEmpty(acceptEncoding))
         {
           acceptEncoding = acceptEncoding.ToUpperInvariant();
           response.Filter = new GZipStream(context.Response.Filter, 
                                 CompressionMode.Compress);
           if (acceptEncoding.Contains("GZIP"))
              {
                 response.AppendHeader("Content-encoding", 
                                       "gzip");
              }
           else if (acceptEncoding.Contains("DEFLATE"))
              {
                  response.AppendHeader("Content-encoding", 
                                        "deflate");
              }
         }
           response.Cache.VaryByHeaders["Accept-Encoding"] = true;           
 }

History

  • 7th May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Mohamed Magdy (Medo)
Web Developer
Egypt Egypt
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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAnother big bugmemberal13n9-Jan-10 0:14 
GeneralIncorrect CachingmemberMember 451353918-May-09 10:15 
General[My vote of 2] There is a big bug in the codememberkatrash13-May-09 3:29 
GeneralSlower than CompressModulemembercecildt12-May-09 3:25 
QuestionUnzipmemberstixoffire11-May-09 22:20 
AnswerRe: UnzipmemberMohamed Magdy (Medo)18-May-09 6:03 
GeneralNeat trick if you can't get access to IISmemberMatware11-May-09 19:49 
GeneralIncomplete!!!!memberzlezj8-May-09 1:42 
GeneralRe: Incomplete!!!!memberMohamed Magdy (Medo)11-May-09 1:11 

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.130619.1 | Last Updated 7 May 2009
Article Copyright 2009 by Mohamed Magdy (Medo)
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid