asp.net-mvc – Gzip压缩无法运行ASP.net MVC5

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

压缩过滤器

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()
{}

并将此类应用于Controller中的Action.但是在firebug中,它仍然显示transfer-encoding:chunked”,但它应该是“transfer-encoding:gzip”.

我在localhost上测试它.

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

更新缓存过滤器工作正常,但仍然没有gzip压缩,下面是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==?=

有什么方法可以让我的工作,我真的需要帮助的人,谢谢

解决方法

如果您无法控制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,"gzip");
            }
        }
    }

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....