Click here to Skip to main content
Licence GPL3
First Posted 7 May 2009
Views 12,248
Bookmarked 20 times

Compress Aspx with GZip

By | 7 May 2009 | Article
A simple function to compress aspx page

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

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAnother big bug Pinmemberal13n0:14 9 Jan '10  
GeneralIncorrect Caching PinmemberMember 451353910:15 18 May '09  
General[My vote of 2] There is a big bug in the code Pinmemberkatrash3:29 13 May '09  
GeneralSlower than CompressModule Pinmembercecildt3:25 12 May '09  
QuestionUnzip Pinmemberstixoffire22:20 11 May '09  
AnswerRe: Unzip PinmemberMohamed Magdy (Medo)6:03 18 May '09  
GeneralNeat trick if you can't get access to IIS PinmemberMatware19:49 11 May '09  
GeneralIncomplete!!!! Pinmemberzlezj1:42 8 May '09  
GeneralRe: Incomplete!!!! PinmemberMohamed Magdy (Medo)1:11 11 May '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 7 May 2009
Article Copyright 2009 by Mohamed Magdy (Medo)
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid