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 7 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
Leave a Comment

(required) 

(required) 

(optional)

(required)