Gzip compression not working ASP.net MVC5

前端 未结 2 577
误落风尘
误落风尘 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:22

    Check your local IIS if Compression is properly configured. Please refer the following link to properly configure IIS for HTTP Compression.

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

    0 讨论(0)
  • 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");
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题