Gzip compression not working ASP.net MVC5

混江龙づ霸主 提交于 2019-12-04 09:32:53

问题


I want to compress my web application with Gzip and I am 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 this class to Action in Controller. But in firebug it's still showing "Transfer-Encoding: chunked" , but it should be "Transfer-Encoding: gzip".

I am testing it on localhost.

Please tell me what am I doing wrong? Thanks and Regards.

update cache filter is working fine, but still no gzip compression, below is 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 any way I can make this work, I really need help guys, Thanks


回答1:


If you can't control IIS, just add this to your Global.ASCX. tested on Android, iPhone, and most 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 the 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");
            }
        }
    }



回答2:


Check your local IIS if Compression is properly configured. Please refer the following link to properly configure IIS for HTTP Compression.

http://www.iis.net/configreference/system.webserver/httpcompression



来源:https://stackoverflow.com/questions/31529076/gzip-compression-not-working-asp-net-mvc5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!