Search code examples
asp.netasp.net-mvcasp.net-mvc-3action-filter

Gzip compression with MVC3 issue on IIS 6


I have some doubts about this server response stream compression on MVC. Actually im using my own action filter attribute for compress.

i attached this CompressFilter into my "Home" action of my HomeController that loads the whole home page, but when i check on firebug i dont see the content-encoding:gzip, even the size is too high 18 KBytes. The url is http://goo.gl/5v5yD and this is the request/response headers:

Response headers
-----------------
Date    Sat, 17 Mar 2012 18:58:49 GMT
Server  Microsoft-IIS/6.0
X-Powered-By    ASP.NET
X-AspNet-Version    4.0.30319
X-AspNetMvc-Version 3.0
Cache-Control   private, max-age=43200
Expires Sun, 18 Mar 2012 06:58:48 GMT
Last-Modified   Sat, 17 Mar 2012 18:58:48 GMT
Content-Type    text/html; charset=utf-8
Transfer-Encoding   chunked

Request headers
-----------------
User-Agent  Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,q=0.8
Accept-Language es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection  keep-alive
Cookie  __utma=72740111.1981468378.1331490472.1331490472.1331490472.1; __utmz=72740111.1331490472.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This is the code of my compression filter:

public class CompressionFilter : ActionFilterAttribute
{
        const CompressionMode compress = CompressionMode.Compress;
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestBase request = filterContext.HttpContext.Request;
            string acceptEncoding = request.Headers["Accept-Encoding"];
            if (string.IsNullOrEmpty(acceptEncoding)) return;
            acceptEncoding = acceptEncoding.ToUpperInvariant();
            HttpResponseBase response = filterContext.HttpContext.Response;
            if (acceptEncoding.Contains("GZIP"))
            {
                response.AppendHeader("Content-encoding", "gzip");
                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
            }
            else if (acceptEncoding.Contains("DEFLATE"))
            {
                response.AppendHeader("Content-encoding", "deflate");
                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
            }
        }
}

Do you know why is not working the compression? im start thinking that maybe is better to try to compress the response using an HttpFilter instead an ActionFilter.


Solution

  • Are you sure? Did you fix it? Maybe your page wasn't refreshed. Ctrl-F5 will do a full refresh. I get the correct response.

    FireFox FireBug:

    Date    Sat, 17 Mar 2012 19:29:58 GMT
    Server  Microsoft-IIS/6.0
    X-Powered-By    ASP.NET
    X-AspNet-Version    4.0.30319
    X-AspNetMvc-Version 3.0
    Content-Encoding    gzip
    Cache-Control   private, max-age=43200
    Expires Sun, 18 Mar 2012 07:29:58 GMT
    Last-Modified   Sat, 17 Mar 2012 19:29:58 GMT
    Content-Type    text/html; charset=utf-8
    Content-Length  4710
    

    Chrome Debug:

    Cache-Control:private, max-age=43200
    Content-Encoding:gzip
    Content-Length:4710
    Content-Type:text/html; charset=utf-8
    Date:Sat, 17 Mar 2012 19:27:20 GMT
    Expires:Sun, 18 Mar 2012 07:27:20 GMT
    Last-Modified:Sat, 17 Mar 2012 19:27:20 GMT
    Server:Microsoft-IIS/6.0
    X-AspNet-Version:4.0.30319
    X-AspNetMvc-Version:3.0
    X-Powered-By:ASP.NET