GZip and Deflate Compression Filter for ASP.Net MVC

I’d previously created a compression processor for dynamic content – using this excellent post for QValues by Dave Transom. Also just just discovered this post on creating an action filter for compression by Kazi Manzur Rashid.

So combining the two and we have:

public class CompressFilter : ActionFilterAttribute
    public override void OnActionExecuting(ActionExecutingContext filterContext)
        HttpRequestBase request = filterContext.HttpContext.Request;

        // load encodings from header
        QValueList encodings = new QValueList(request.Headers["Accept-Encoding"]);

        // get the types we can handle, can be accepted and in the defined client preference
        QValue preferred = encodings.FindPreferred("gzip", "deflate", "identity");

        // if none of the preferred values were found, but the
        // client can accept wildcard encodings, we'll default
        // to Gzip.
        if (preferred.IsEmpty && encodings.AcceptWildcard && encodings.Find("gzip").IsEmpty)

            preferred = new QValue("gzip");

        HttpResponseBase response = filterContext.HttpContext.Response;
        // handle the preferred encoding
        switch (preferred.Name)
            case "gzip":
                response.AppendHeader("Content-encoding", "gzip");
                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);

            case "deflate":
                response.AppendHeader("Content-encoding", "deflate");
                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);

            case "identity":

Which when applied as below to any action method will enable compression if it’s supported by the browser.

public ContentResult SiteMap()
    return new ContentResult
                   Content = SyndicationHelper.GetAggregateSiteMap(),
                   ContentType = "application/xml; charset=UTF-8"

Download here: QValueAndCompressionFilter.zip



maybe you should priorities deflate over gzip as gzip is essentially a padded deflate, eg. more bytes for no use

Thanks for the feedback Andy and 'Anon'.

I've tested the compression filter with all the major browser version, Safari, Firefox, Chrome, Opera and Internet Explorer 6 SP2 and above, and they all work fine with GZip or Deflate compressed content. Given the current usage statistics on the Web for browser versions I think it's probable safe at this point to serve compressed content without specific user agent checks. Although of course it depends on the application - and implementers may choose otherwise.

I test all controll by CompressFile but it has a rand for zip.becuse if size of controll be small compressfill cannot under from controllsize with gzip

I have integrated this code with my MVC.net application. I can see compressed file for firefox, but could not see reduced size for chrome or IE (7 or 8). Do I need to change any settings? Am I doing something wrong?