我對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來壓縮響應。
你爲什麼不IIS內置的壓縮功能? – tugberk 2012-03-17 19:32:08
因爲我使用IIS 6.我認爲它沒有它,對吧? 謝謝。 – Jose3d 2012-03-17 20:03:05