Click here to Skip to main content
15,890,982 members
Articles / Web Development / ASP.NET

Compress Aspx with GZip

Rate me:
Please Sign up or sign in to vote.
3.67/5 (4 votes)
7 May 2009GPL3 30.4K   22   9
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: 

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

The method:

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


Written By
Web Developer
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAnother big bug Pin
al13n9-Jan-10 0:14
al13n9-Jan-10 0:14 
GeneralIncorrect Caching Pin
Member 451353918-May-09 10:15
Member 451353918-May-09 10:15 
General[My vote of 2] There is a big bug in the code Pin
katrash13-May-09 3:29
katrash13-May-09 3:29 
This method has a big flaw if the header has the two schemes listed as:

Accept-Encoding: deflate, gzip

The code should prioritize the compression according to the appearance of the encoding schemes in the header; the sender expects the server to support the scheme that comes first in the header first thing.

Moreover, for non IIS hosting (such as using the host that comes with VS development), this compression will not work if the resource is a javascript file. So, the method should use filtering by the type of the requst and filter the as?z pages only by using RegEx to check if the page is any of ashx, ascx, etc. (i.e., new Regex(@"\.as[hmp]x$"))

BTW, this example is taken from this book:
Professional ASP.NET 2.0 / Bill Evjen ... et al.
ISBN: 0-7645-7610-0
www.wrox.com
Chapter 22
GeneralSlower than CompressModule Pin
cecildt12-May-09 3:25
cecildt12-May-09 3:25 
QuestionUnzip Pin
stixoffire11-May-09 22:20
stixoffire11-May-09 22:20 
AnswerRe: Unzip Pin
Mohamed Magdy (Medo)18-May-09 6:03
Mohamed Magdy (Medo)18-May-09 6:03 
GeneralNeat trick if you can't get access to IIS Pin
Matware11-May-09 19:49
Matware11-May-09 19:49 
GeneralIncomplete!!!! Pin
zlezj8-May-09 1:42
zlezj8-May-09 1:42 
GeneralRe: Incomplete!!!! Pin
Mohamed Magdy (Medo)11-May-09 1:11
Mohamed Magdy (Medo)11-May-09 1:11 

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.