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 »
May 3rd, 2009 at 9:27 pm
[...] 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 [...]
May 4th, 2009 at 10:55 pm
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..
May 5th, 2009 at 10:09 am
That’s a great catch, thanks…I’ll update the post.
May 11th, 2009 at 1:14 pm
[...] Easy Compression with ASP.NET MVC – The Grubbsian (Suggested by Elijah Manor) [...]
May 11th, 2009 at 1:14 pm
[...] Easy Compression with ASP.NET MVC – The Grubbsian (Suggested by Elijah Manor) [...]
May 12th, 2009 at 4:52 am
Couldn’t you just use compression at the IIS level?
May 12th, 2009 at 5:06 am
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!
May 12th, 2009 at 7:03 pm
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.
May 13th, 2009 at 10:37 am
[...] Twitter! RT @shijucv: Easy Compression with ASP.NET MVC http://www.thegrubbsian.com... #aspnetmvc12:58 AM May 11th from TweetDeck mahdi Mahdi [...]
September 14th, 2009 at 5:00 pm
[...] Easy Compression with ASP.NET MVC — The Grubbsian (9/14/2009) [...]