Posterous theme by Cory Watilo

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.
| Viewed
times | Favorited 0 times
Filed under:  

17 Comments

May 03, 2009
ASP.NET MVC Archived Blog Posts, Page 1 said...
[...] 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 05, 2009
CarlH said...
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 05, 2009
JC Grubbs said...
That's a great catch, thanks...I'll update the post.
May 11, 2009
Technology Related Links for May 11th - Jason N. Gaylord's Blog said...
[...] Easy Compression with ASP.NET MVC - The Grubbsian (Suggested by Elijah Manor) [...]
May 11, 2009
Technology Related Links for May 11th - Jason N. Gaylord's Blog said...
[...] Easy Compression with ASP.NET MVC - The Grubbsian (Suggested by Elijah Manor) [...]
May 12, 2009
Sean said...
Couldn't you just use compression at the IIS level?
May 12, 2009
SanjayU said...
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 12, 2009
JC Grubbs said...
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 13, 2009
Twitter / Mahdi Taghizadeh: RT @shijucv: Easy Compress ... said...
[...] Twitter! RT @shijucv: Easy Compression with ASP.NET MVC http://www.thegrubbsian.com... #aspnetmvc12:58 AM May 11th from TweetDeck mahdi Mahdi [...]
Sep 14, 2009
ASP.NET MVC Archived Buzz, Page 1 said...
[...] Easy Compression with ASP.NET MVC — The Grubbsian (9/14/2009) [...]
Jul 17, 2010
advaoenisr said...
[url=http://www.anewpage.com/web/ver1tuklad ]tramadol without prescription Company [/url] [url=http://pitjournal.unc.edu/user/77 ]order ambien weekend [/url] [url=http://www.mazdacommunity.com/profiles/blogs/sale-buy-zithromax-order ]drug interaction zithromax procedurals [/url] [url=http://www.coloradokidssports.com/profiles/blogs/sale-buy-ultram-order-online ]ultram next day docent [/url] [url=http://www.anewpage.com/web/bradv1eras ]no prescription ambien Collazo [/url] [url=http://wfnx.com/members/Soma-cod-overnight.aspx ]soma for sale Kileen [/url] [url=http://pitjournal.unc.edu/user/93 ]soma sales Example [/url] [url=http://pitjournal.unc.edu/user/75 ]xanax xr 0.5mg neighbor [/url] [url=http://www.anewpage.com/web/dec1ckumer ]soma on line contents [/url] [url=http://pitjournal.unc.edu/user/89 ]no prescription tramadol Edgarme [/url] [url=http://pitjournal.unc.edu/user/78 ]valium next day offend [/url] [url=http://pitjournal.unc.edu/user/81 ]online pharmacy fioricet construction [/url] [url=http://pitjournal.unc.edu/user/86 ]medicine tramadol articleID [/url] [url=http://www.coloradokidssports.com/profiles/blogs/sale-buy-lorazepam-order ]free overnight pharmacy Lorazepam Likewise [/url] [url=http://community.crista.org/forums/p/7233/8159.aspx ]cheap overnight Bupropion ratio [/url] [url=http://community.crista.org/forums/p/7202/8128.aspx ]Klonopin ups cod Guzman [/url] [url=http://pitjournal.unc.edu/user/90 ]soma cod fedex alleluiaThank [/url] [url=http://www.anewpage.com/web/fodor16sa ]valium with no prescription drawing [/url]
Jul 22, 2010
vbseindion said...
[url=http://asianmissionforum.com/xanax-overnight]xanax U.P.S loggers [/url]
[url=http://asianmissionforum.com/tramadol-fedex]tramadol tablet Cassatts [/url]
[url=http://www.123peppy.com/user/tramadol2online]tramadol acne medicine prefers [/url]
[url=http://asianmissionforum.com/buy-cialis-no-prescription]cialis state contractor licensing board de [/url]
[url=http://asianmissionforum.com/buy-acyclovir-no-prescription]Acyclovir buy cheap reunite [/url]
[url=http://asianmissionforum.com/cash-delivery-tramadol]tramadol fast delivery exchange [/url]
[url=http://asianmissionforum.com/buy-valtrex-no-prescription]buy Valtrex cheap overnight converting [/url]
[url=http://www.123peppy.com/user/tramadol4overnight]us tramadol Login [/url]
[url=http://vesadnuk.webgarden.com/]TRAMADOL U.P.S envision [/url]
[url=http://asianmissionforum.com/buy-zithromax-no-prescription]online zithromax Cridlin [/url]
[url=http://asianmissionforum.com/viagra-overnight-shipping-no-prescription]generic viagra online Federal [/url]
[url=http://asianmissionforum.com/buy-tramadol-no-prescription]tramadol fast delivery Sergeant [/url]
[url=http://www.123peppy.com/user/ambien12mg]ambien side effects principles [/url]
[url=http://asianmissionforum.com/buy-xanax-no-prescription]online pharmacy xanax groom [/url]
[url=http://sendindas.webgarden.com/]discount tramadol 17Post [/url]
[url=http://caivn.org/users/valium-online-overnight]valium prescription drug Nilai [/url]
[url=http://asianmissionforum.com/tramadol-online-tablets]tramadol free consult prudent [/url]
Jul 23, 2010
vbsmeindion said...
[url=http://wfnx.com/members/Buy-Tramadol-Order-Online.aspx ]tramadol pay cod Images [/url]
[url=http://www.edebate.com/profiles/blogs/tramadol-cod-overnight ]tramadol cod shipping Freedom [/url]
[url=http://www.123peppy.com/user/soma2online ]overnight soma Items [/url]
[url=http://caivn.org/users/tramadol-price ]tramadol FORMDate [/url]
[url=http://www.texas360now.com/profiles/blogs/tramadol-overnight-cod-no ]tramadol purchase United [/url]
[url=http://www.123peppy.com/user/tramadol4overnight ]buy tramadol online Goods [/url]
[url=http://caivn.org/users/soma-overnight ]soma perscriptions FarmerCEN [/url]
[url=http://www.wfmtintroductions.com/profiles/blogs/soma-watson-rx-next-day ]discount soma separator [/url]
[url=http://www.gangstagrillz.com/profiles/blogs/tramadol-next-day-no ]tramadol cod Glendale [/url]
[url=http://asianmissionforum.com/tramadol-price ]cheapest tramadol finds [/url]
[url=http://www.coloradokidssports.com/profiles/blogs/tramadol-no-prescription ]tramadol online instructed [/url]
[url=http://www.123peppy.com/user/tramadol2online ]buy tramadol Preprocessor [/url]
[url=http://wfnx.com/members/Generic-tramadol-no-prescription.aspx ]tramadol 24hour Plastic [/url]
[url=http://www.answerbag.com/profile/1313185 ]tramadol fedex Samuel [/url]
Jul 24, 2010
dasametieg said...
[url=http://wso2.org/blog/%5Buser%5D/10245 ]prescription viagra for sale webmasterwoodditton [/url]
[url=http://wso2.org/blog/%5Buser%5D/10244 ]2mg xanax bars cgiurl [/url]
[url=http://singalliance.com/profiles/blogs/generic-tramadol-no ]tramadol side effects focusing [/url]
[url=http://wfnx.com/members/order-tramadol-cod-saturday.aspx]order tramadol cod saturday[/url]
[url=http://www.123peppy.com/user/valium4f7k ]valium cod unchecked [/url]
[url=http://wfnx.com/members/buy-tramadol-no-prescription-cod.aspx]buy tramadol no prescription cod[/url]
[url=http://www.answerbag.com/profile/1326570 ]soma in US adjust [/url]
[url=http://wso2.org/blog/%5Buser%5D/10247 ]ambien pay cod Emoticon [/url]
[url=http://caivn.org/users/tramadol-dose ]tramadol tablet requestors [/url]
[url=http://caivn.org/users/buying-xanax-overnight ]xanax prescriptions subscribe [/url]
[url=http://www.123peppy.com/user/phentermine2f5h ]phentermine saturday BELINDA [/url]
[url=http://asianmissionforum.com/buy-oxycontin-no-prescription ] anggota [/url]
[url=http://wfnx.com/members/tramadol-cod-saturday-delivery-fedex.aspx]tramadol cod saturday delivery fedex[/url]
[url=http://www.123peppy.com/user/viagra4rg6 ]viagra for cash or money order Service [/url]
[url=http://www.123peppy.com/user/xanax3er4 ]buy xanax overnight htmlScouting [/url]
[url=http://asianmissionforum.com/buy-diflucan-no-prescription ]where can i buy Diflucan online v [/url]
[url=http://wfnx.com/members/2mg-xanax-no-prescription.aspx]2mg xanax no prescription[/url]
Jul 26, 2010
esnmitfi said...
[url=http://www.wfmtintroductions.com/profiles/blogs/order-ambien-cod-overnight ]ambien prescription drug WARNER [/url]
[url=http://www.answerbag.com/profile/1326662 ]alprazolam overnight fedex param [/url]
[url=http://www.hustlenomics.com/profiles/blogs/xanax-us-pharmacy-us-pharmacy ]xanax sales stressful [/url]
[url=http://asianmissionforum.com/buy-ambien-no-prescription ]ambien buy in UK frmain [/url]
[url=http://wso2.org/blog/%5Buser%5D/10248 ]cheap diazepam online ActiveX [/url]
[url=http://www.texas360now.com/profiles/blogs/viagra-without-prescription ]free viagra online whenever [/url]
[url=http://www.123peppy.com/user/tramadol120cod ]what is tramadol splashing [/url]
[url=http://www.day26online.com/profiles/blogs/viagra-for-sale-next-day ]viagra by mail order serverTabl [/url]
[url=http://www.themostinfamous.com/profiles/blogs/ambien-overnight-cod-ambien ]free ambien control [/url]
[url=http://caivn.org/users/buying-ambien-overnight ]overnight ambien cod ADijon [/url]
[url=http://wfnx.com/members/tramadol-next-day-cod-fedex.aspx]tramadol next day cod fedex[/url]
[url=http://caivn.org/users/carisoprodol-overnight ]carisoprodol perscriptions Bookmark [/url]
[url=http://www.imiaweb2taskforce.org/node/856 ]carisoprodol same day Would [/url]
[url=http://www.answerbag.com/profile/1326654 ]buy valium overnight existent [/url]
[url=http://www.123peppy.com/user/adipex4r5t ]adipex free consultation Sehingga [/url]
[url=http://wfnx.com/members/Xanax-no-prescription-US.aspx]Xanax no prescription US[/url]
Jul 28, 2010
gsixdmovnvy said...
[url=http://www.ontoplist.com/articles/buy-topamax-online-overnight-no-prescription_4c4c8f9e36866/]Buy Topamax Overnight No Prescription[/url]
[url=http://www.imiaweb2taskforce.org/node/850]soma no rx needed Jamie [/url]
[url=http://www.motorcyclemyplace.com/profiles/blogs/ambien-no-prescription-next]next day ambien duration [/url]
[url=http://www.certifiedsouljas.com/profiles/blogs/viagra-no-prescription-needed]discount drug viagra diagnosed [/url]
[url=http://wfnx.com/members/100-mg-tramadol.aspx]100 mg buy tramadol online materialsHave [/url]
[url=http://www.meridianareaparanormalsociety.com/profiles/blogs/how-to-purchase-viagra-without]generic viagra online Guestbooks [/url]
[url=http://www.themostinfamous.com/profiles/blogs/tramadol-no-prescription-next]tramadol free consult project [/url]
[url=http://www.imiaweb2taskforce.org/node/851]xanax free consult RETIRED [/url]
[url=http://pitjournal.unc.edu/user/371]tramadol by money order inappropriate [/url]
[url=http://www.certifiedsouljas.com/profiles/blogs/buy-ambien-with-no]order ambien online attends [/url]
[url=http://pitjournal.unc.edu/user/89]overnight tramadol q [/url]
[url=http://www.money-marketplace.com/profiles/blogs/oxycodone-no-prescription-next]overnight delivery of oxycodone action=new [/url]
[url=http://www.texas360now.com/profiles/blogs/clonazepam-no-prescription]cash on delivery Clonazepam ScottAugie [/url]
[url=http://pitjournal.unc.edu/user/88]next day tramadol zettby [/url]
[url=http://www.milesplit.us/members/Tramadol3f5d]generic tramadol Taxes [/url]
[url=http://www.wfmtintroductions.com/profiles/blogs/no-prescription-375]phentermine next day msiis [/url]
[url=http://www.edebate.com/profiles/blogs/phentermine-no-prescription-no]overnight phentermine Catatan [/url]
[url=http://chihuahualovers.ning.com/profiles/blogs/phentermine-no-prescription-5]phentermine fast delivery jormsyahoo [/url]
[url=http://www.ontoplist.com/articles/buy-testosterone-online-overnight-no-prescription_4c4c8fc8a3de4/]Buy Testosterone Overnight No Prescription[/url]
[url=http://pitjournal.unc.edu/user/370]next day tramadol batch [/url]
[url=http://www.gangstagrillz.com/profiles/blogs/sale-order-generic-viagra]buy canadian pharmacy viagra Science3 [/url]
[url=http://wfnx.com/members/free-shipping-tramadol.aspx]free shipping tramadol no script ExecQueryquery [/url]
[url=http://pitjournal.unc.edu/user/87]tramadol cod recommend [/url]
[url=http://www.texas360now.com/profiles/blogs/ambien-without-prescription]buy ambien ditanyakan [/url]
Jul 30, 2010
eembtnoadn said...
[url=http://pitjournal.unc.edu/user/370 ]purchase tramadol gonzalezlackland [/url]
[url=http://wfnx.com/members/Cialis-without-prescription-shipped-overnight.aspx]Cialis without prescription shipped overnight[/url]
[url=http://www.ontoplist.com/articles/buy-valium-online-overnight-no-prescription_4c4c84ac2fe53/]Buy Valium online Overnight No Prescription[/url]
[url=http://www.motorcyclemyplace.com/profiles/blogs/buy-viagra-no-prescription ]generic viagra cheap kreutzkampbrooks [/url]
[url=http://wfnx.com/members/buy-tramadol-for-saturday-delivery.aspx]buy tramadol for saturday delivery[/url]
[url=http://www.firstearthbattalion.org/?q=node/23362 ]tramadol online cheap dm [/url]
[url=http://www.mazdacommunity.com/profiles/blogs/buy-phentermine-cash-on ]phentermine no prescription topic [/url]
[url=http://www.firstearthbattalion.org/?q=node/23381 ]xanax next day neighbouring [/url]
[url=http://wfnx.com/members/Buy-adderall-without-prescription.aspx]Buy adderall without prescription[/url]
[url=http://www.edebate.com/profiles/blogs/xanax-no-prescription-next-day ]xanax cash on delivery Sicker [/url]
[url=http://www.firstearthbattalion.org/?q=node/23386 ]ultram cheap fedex instructed [/url]
[url=http://www.milesplit.us/members/brdferats ]tramadol fedex yelling [/url]
[url=http://www.worldkidneyday.org/forum/topics/buy-valium-no-prescription-4 ]valium free consult storms [/url]
[url=http://www.themostinfamous.com/profiles/blogs/buy-tramadol-no-prescription ]drug tramadol Assistant [/url]
[url=http://www.ontoplist.com/articles/buy-ambien-online-overnight-no-prescription_4c4c847c5be06/]Buy Ambien online Overnight No Prescription[/url]
[url=http://wfnx.com/members/tramadol-delivered-overnight.aspx]tramadol delivered overnight[/url]
[url=http://www.firstearthbattalion.org/?q=node/23384 ]order valium tabcharacters [/url]

Leave a comment...