Gzip压缩不起作用ASP.net MVC5 [英] Gzip compression not working ASP.net MVC5

查看:174
本文介绍了Gzip压缩不起作用ASP.net MVC5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to compress my web application with Gzip and I am using following 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()
{}

并将此类应用于Controller中的Action.但是在Firebug中,它仍显示传输编码:已分块" ,但应为传输编码:gzip" .

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".

我正在localhost上对其进行测试.

请告诉我我在做什么错?谢谢并恭祝安康.

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

更新缓存过滤器工作正常,但仍未进行gzip压缩,下面是chrome中的响应标头.

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

推荐答案

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

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

这篇关于Gzip压缩不起作用ASP.net MVC5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆