asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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(datetiR_243_11845@e.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");
            }
        }
    }

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc – Gzip压缩无法运行ASP.net MVC5全部内容,希望文章能够帮你解决asp.net-mvc – Gzip压缩无法运行ASP.net MVC5所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。