2012-03-17 99 views
1

我对MVC上的此服务器响应流压缩存在一些疑问。其实即时通讯使用我自己的操作过滤属性进行压缩。IIS上的MVC3问题Gzip压缩6

我将这个CompressFilter附加到我的HomeController的“Home”操作中,该操作加载了整个主页,但是当我检查Firebug时,我看不到内容编码: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来压缩响应。

+0

你为什么不IIS内置的压缩​​功能? – tugberk 2012-03-17 19:32:08

+1

因为我使用IIS 6.我认为它没有它,对吧? 谢谢。 – Jose3d 2012-03-17 20:03:05

回答

1

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

火狐萤火:

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 
+0

我尝试了一切Ctrl + F5 Ctrl + R,删除所有的浏览器缓存,数据,无论...非常感谢,然后是Gzipped! – Jose3d 2012-03-17 19:35:53

+0

@ Jose3d - 嗯,我讨厌它,当我发生这种事时!有时浏览器不会放过它:(尽管如此,你的代码工作得很好:) – 2012-03-17 19:37:22