Jaycent Drysdale

asp.net mvc page compression

Looking for a great way to reduce page size and reduce bandwith usage on your asp.net mvc web application?
consider looking into page compression. Below is the c# class that I used to knock my page size down from 44K to a measly 6K...thus saving on bandwidth usage and page download time. 

First, you need to reference the following namespaces

 

using System.IO.Compression;
using System.Web;
using System.Web.Mvc;


 

And then you throw this class together, subclassed by ActionFilterAttribute

public class EnableCompressionAttribute : ActionFilterAttribute
{
  
const CompressionMode compress = CompressionMode.Compress;
  
  
public override void OnActionExecuting(ActionExecutingContext filterContext)
   {
      
HttpRequestBase request = filterContext.HttpContext.Request;
       HttpResponseBase response = filterContext.HttpContext.Response;
       string acceptEncoding = request.Headers["Accept-Encoding"];
       if (acceptEncoding == null)
          
return;
      
else if (acceptEncoding.ToLower().Contains("gzip"))
       {
             response.Filter =
new GZipStream(response.Filter, compress);
             response.AppendHeader(
"Content-Encoding", "gzip");
       }
      
else if (acceptEncoding.ToLower().Contains("deflate"))
       {
             response.Filter =
new DeflateStream(response.Filter, compress);
             response.AppendHeader(
"Content-Encoding", "deflate");
       }
   }
}

then decorate your Controller Methods like so:

[EnableCompression]
public ActionResult Index(string id)
{
......
}

Refresh your page and be prepared to be knowcked off your rockers in terms of the dramatic chnage in page size.

Now this should work in all modern browsers, but for some reason, the latest distribution of Internet Explorer is totally out of the loop as far as this goes.
But it does work with latest version of Opera, Firefox, Safari, Chrome. 

For further reading, check out this article regarding page compression.
http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

  

 

Posted: Feb 21 2010, 07:00 AM by jaycent | with 13 comment(s)
Filed under: ,

Comments

George said:

Sounds great, thanks for sharing.

# February 21, 2010 8:34 PM

Arun Mahendrakar said:

Hey Jaycent.. this is really nice.. i'm gonna tweet about this (of course with your name!).

Arun

# February 21, 2010 9:31 PM

webbes said:

Hi,

Why is this better than to enable compression in IIS?

Cheers,

Wesley

# February 22, 2010 7:57 AM

jaycent said:

@webbes: if you host your website on a shared hosting server (like godaddy.com), you wont have access to the IIS setting that enables compression. In such case, the solution I presented above would come in handy.

# February 22, 2010 3:20 PM

jaycent said:

@Arun: Sure, go ahead!

# February 22, 2010 3:30 PM

leton said:

Thanks for your article.

My FF version 3.6.3 - Its can not show the output.

how can solve this problem? Have to any configure my

IIS Server ? Please help me.

# April 19, 2010 8:28 AM

Twitter Mirror said:

http://weblogs. asp.net /jaycentdrysdale/archive/2010/02/21/asp-net-mvc-page-compression.aspx

# June 1, 2010 6:18 PM

Ireland said:

I¡¦m not certain that the place you are getting your information, however great topic.

I needs to spend some time learning more or understanding more.

Thanks for magnificent info I used to be in lookup of this

info for my mission.

# December 22, 2012 1:29 AM

Crain said:

Right now it seems for instance Movable Type is the best blogging platform

available right now. (out of what I’ve read) Is that what you’re using on your blog?

# January 8, 2013 5:53 PM

Strickland said:

inkheart has great graphics in spite of this the story seriously isn't that very impressive;

# January 8, 2013 6:32 PM

Matthews said:

I?ve been exploring for a tiny for any high quality articles or weblog posts on this

kind of space . Exploring in Yahoo I finally stumbled upon this website.

Reading this info So i?m satisfied to convey that I have a very fine uncanny feeling I

found out just what I needed. I so much no doubt will make sure

to do not overlook this site and provides it a glance regularly.

# January 12, 2013 12:42 AM

Devine said:

The best and evident News and why it technique a large amount.

# January 29, 2013 10:38 PM

pills for lose weight said:

pUk026 Really appreciate you sharing this post.

# February 1, 2013 5:01 AM