J
J
JoraInTheSky2017-08-27 12:44:57
ASP.NET
JoraInTheSky, 2017-08-27 12:44:57

Compression (Compress) in ASP.NET MVC?

There are several ways to compress in ASP.NET MVC:

  1. adding the Application_BeginRequest method to Global.ASCX
    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");
                }
            }
        }

  2. Through filters:
    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);
            }
        }
    }

    Well, then hang the necessary methods, etc.:
    [CompressFilter]
    public ActionResult Index()
    {}

  3. Adding appropriate instructions to Web.Config:
    <system.webServer>
        <httpCompression>
          <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
          <dynamicTypes>
            <add mimeType="text/*" enabled="true"/>
            <add mimeType="message/*" enabled="true"/>
            <add mimeType="application/javascript" enabled="true"/>
            <add mimeType="*/*" enabled="false"/>
          </dynamicTypes>
          <staticTypes>
            <add mimeType="text/*" enabled="true"/>
            <add mimeType="message/*" enabled="true"/>
            <add mimeType="application/javascript" enabled="true"/>
            <add mimeType="*/*" enabled="false"/>
          </staticTypes>
        </httpCompression>
        <urlCompression doStaticCompression="true" doDynamicCompression="true"/>
      </system.webServer>


1. What is the difference between the first and second way? In addition to the fact that the first compresses everything, and the second only what is marked with the corresponding attribute.
2. Quality of compression between all three methods?
3. If you just enable compression in Web.Confige
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
then the compression will be the same as in the 3rd method?
4. The main question is which method and when is it used?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Antonenko, 2017-09-07
@JoraInTheSky

  1. The difference is at what stage the compression filter will be added + as you noted in the second option, you yourself control which Actions will be compressed
  2. No difference
  3. Almost, the target mime-types will be taken from the IIS config - it may differ from the one specified in point #3
  4. Do not forget that compression consumes a resource - CPU, but reduces the amount of traffic. The specific solution depends on your requirements and the problem you are trying to solve. Option #1 always loses to #2 in flexibility. Only option #3 allows you to compress static. If the goal is to compress everything - use #2 (register globally) and/or #3. If separate Actions which return big pages - №2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question