Gzip 压缩不起作用 ASP.net MVC5

Gzip compression not working ASP.net MVC5

我想用 Gzip 压缩我的 Web 应用程序,我正在使用以下 class

压缩过滤器

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);
        }
    }
}

缓存过滤器

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");
    }
}

控制器

[CompressFilter]
[CacheFilter(Duration = 60)]
public ActionResult Index()
{}

并将此 class 应用于 Controller 中的 Action。但是在 firebug 它仍然显示 "Transfer-Encoding: chunked" ,但它应该是 "Transfer-Encoding: gzip".

我正在本地主机上测试它。

请告诉我我做错了什么?感谢致敬。

更新 缓存过滤器工作正常,但仍然没有 gzip 压缩,下面是 chrome.

中的响应 header
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==?=

有什么方法可以让我完成这项工作,我真的需要帮助,谢谢

如果压缩配置正确,请检查您的本地 IIS。请参考以下 link 以正确配置 IIS 以进行 HTTP 压缩。

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

如果您无法控制 IIS,只需将此添加到您的 Global.ASCX。在 Android、iPhone 和大多数 PC 浏览器上进行了测试。

 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");
            }
        }
    }