Gzip compression not working ASP.net MVC5

前端 未结 2 576
误落风尘
误落风尘 2020-12-30 12:55

I want to compress my web application with Gzip and I am using following class

compression filter

public class CompressFilter : Acti         


        
2条回答
  •  隐瞒了意图╮
    2020-12-30 13:41

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

提交回复
热议问题