2012-03-17 122 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