【问题标题】:Gzip compression with MVC3 issue on IIS 6Gzip 压缩与 IIS 6 上的 MVC3 问题
【发布时间】:2012-03-17 19:09:58
【问题描述】:

我对 MVC 上的这种服务器响应流压缩有一些疑问。实际上我使用我自己的动作过滤器属性进行压缩。

我将此 CompressFilter 附加到加载整个主页的 HomeController 的“Home”操作中,但是当我检查 firebug 时,我没有看到 content-encoding:gzip,即使大小太高 18 KB。网址是http://goo.gl/5v5yD,这是请求/响应标头:

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)

这是我的压缩过滤器的代码:

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);
            }
        }
}

你知道为什么压缩不工作吗?我开始认为尝试使用 HttpFilter 而不是 ActionFilter 来压缩响应可能会更好。

【问题讨论】:

  • 你的IIS为什么不自带压缩功能?
  • 因为我使用的是 IIS 6。我认为它没有,对吧?谢谢。

标签: asp.net asp.net-mvc asp.net-mvc-3 action-filter


【解决方案1】:

你确定吗?你修好了吗?也许您的页面没有刷新。 Ctrl-F5 将进行完全刷新。我得到了正确的回应。

FireFox 萤火虫:

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 调试:

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

【讨论】:

  • 我尝试了一切 Ctrl+F5 Ctrl+R,删除所有浏览器缓存,数据,等等...非常感谢,然后被压缩了!!!
  • @Jose3d - 嗯,我也讨厌这种事发生在我身上!有时浏览器就是不放手 :( 干得好,你的代码工作得很好 :)