Easy Compression with ASP.NET MVC

ASP.NET MVC already comes with great performance out of the box…due mostly to the lightweight nature of the framework. However, in the never-ending pursuit of faster load times I starting looking into what it would take to put together an Action Filter to compress the output of a controller action. Turns out, it’s pretty damn easy. Here’s what I came up with:

public class CompressAttribute : ActionFilterAttribute {
 
        public override void OnActionExecuting(ActionExecutingContext filterContext) {
 
            var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
            if (string.IsNullOrEmpty(encodingsAccepted)) return;
 
            encodingsAccepted = encodingsAccepted.ToLowerInvariant();
            var response = filterContext.HttpContext.Response;
 
            if (encodingsAccepted.Contains("deflate")) {
                response.AppendHeader("Content-encoding", "deflate");
                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
            } else if (encodingsAccepted.Contains("gzip")) {
                response.AppendHeader("Content-encoding", "gzip");
                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
            }
        }
    }

And you can use it like this:

[Compress]
public class ThingController : Controller { ... }

or on a controller action:

public class ThingController: Controller {
 
    [Compress]
    public ActionResult Index() { ... }
}

As you can see it really just sets a new filter for the response; GZipStream or DeflateStream depending on what the browser can handle and then sets the appropriate flag. After doing some lightweight testing it looks like the compressed output is on average 18% of the original size. My next task is to do some testing around how long the client takes to actually decompress the response. My guess is that it’s pretty negligible though.

Category: ASP.NET MVC 10 comments »

10 Responses to “Easy Compression with ASP.NET MVC”

  1. ASP.NET MVC Archived Blog Posts, Page 1

    [...] to VoteEasy Compression with ASP.NET MVC (5/2/2009)Saturday, May 02, 2009 from JC GrubbsASP.NET MVC already comes with great performance out of the [...]

  2. CarlH

    You should change place of the gzip and deflate if-statement, as gzip is more or less just a padded deflate, eg. more bytes for no use..

  3. JC Grubbs

    That’s a great catch, thanks…I’ll update the post.

  4. Technology Related Links for May 11th - Jason N. Gaylord's Blog

    [...] Easy Compression with ASP.NET MVC – The Grubbsian (Suggested by Elijah Manor) [...]

  5. Technology Related Links for May 11th - Jason N. Gaylord's Blog

    [...] Easy Compression with ASP.NET MVC – The Grubbsian (Suggested by Elijah Manor) [...]

  6. Sean

    Couldn’t you just use compression at the IIS level?

  7. SanjayU

    This would be the same net effect as turning on HTTP Compression either in the web.config (IIS7) or meta-base (IIS6), correct?

    Using the web.config/metabase configuration approach to HTTP compression it seems like it provides a bit more flexibility since you can explicitly compress files based on format as well as provide a compression rate.

    Thanks for the great post!

  8. JC Grubbs

    You could, but it’s nice to be able to choose what URL’s are going to cause compression and which will not at a pretty detailed level. I suspect that you could achieve the same thing through configuring IIS in clever ways, but this is another option.

  9. Twitter / Mahdi Taghizadeh: RT @shijucv: Easy Compress ...

    [...] Twitter! RT @shijucv: Easy Compression with ASP.NET MVC http://www.thegrubbsian.com... #aspnetmvc12:58 AM May 11th from TweetDeck mahdi Mahdi [...]

  10. ASP.NET MVC Archived Buzz, Page 1

    [...] Easy Compression with ASP.NET MVC — The Grubbsian (9/14/2009) [...]


Leave a Reply



Back to top