i want compress web application gzip , using following class
compression filter
public class compressfilter : actionfilterattribute { 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); } } } cache filter
public class cachefilterattribute : actionfilterattribute { public int duration { get; set; } public cachefilterattribute() { duration = 1; } public override void onactionexecuted(actionexecutedcontext filtercontext) { if (duration <= 0) return; httpcachepolicybase cache = filtercontext.httpcontext.response.cache; timespan cacheduration = timespan.fromminutes(duration); cache.setcacheability(httpcacheability.public); cache.setexpires(datetime.now.add(cacheduration)); cache.setmaxage(cacheduration); cache.appendcacheextension("must-revalidate, proxy-revalidate"); } } controller
[compressfilter] [cachefilter(duration = 60)] public actionresult index() {} and applying class action in controller. in firebug it's still showing "transfer-encoding: chunked" , should "transfer-encoding: gzip".
i testing on localhost.
please tell me doing wrong? , regards.
update cache filter working fine, still no gzip compression, below response header in chrome.
cache-control:public, must-revalidate, proxy-revalidate, max-age=3600 content-type:text/html; charset=utf-8 date:wed, 22 jul 2015 13:39:06 gmt expires:wed, 22 jul 2015 14:39:04 gmt server:microsoft-iis/10.0 transfer-encoding:chunked x-aspnet-version:4.0.30319 x-aspnetmvc-version:5.1 x-powered-by:asp.net x-sourcefiles:=?utf-8?b?qzpcvxnlcnncqxjiyxpcrg9jdw1lbnrzxfzpc3vhbcbtdhvkaw8gmjaxm1xqcm9qzwn0c1xidxlwcmljzxnwywtpc3rhblxcdxlqag9uzq==?= is there way can make work, need guys, thanks
if can't control iis, add global.ascx. tested on android, iphone, , pc browsers.
protected void application_beginrequest(object sender, eventargs e) { // implement http compression httpapplication app = (httpapplication)sender; // retrieve accepted encodings string encodings = app.request.headers.get("accept-encoding"); if (encodings != null) { // check browser accepts deflate or gzip (deflate takes preference) encodings = encodings.tolower(); if (encodings.contains("deflate")) { app.response.filter = new deflatestream(app.response.filter, compressionmode.compress); app.response.appendheader("content-encoding", "deflate"); } else if (encodings.contains("gzip")) { app.response.filter = new gzipstream(app.response.filter, compressionmode.compress); app.response.appendheader("content-encoding", "gzip"); } } }
Comments
Post a Comment