Skip to main content

How to enable gzip compression in ASP.NET Core

ASP.NET Core 1.1 has an inbuilt middleware for Response compression, which by default uses gzip compression. All modern browsers support response compression, and you should take advantage of it. Instead of sending response from the server as it is, it’s better to compress it and then send it, as this will reduce the response size and provides better speed. So in this post, let’s see how to enable gzip compression in ASP.NET Core.

Enable gzip compression in ASP.NET Core

To use this middleware, make sure you have ASP.NET 1.1 installed. Download and install the .NET Core 1.1 SDK. Let’s create an ASP.NET Core web API application. Open Project.json and include following nuget package.

"Microsoft.AspNetCore.ResponseCompression": "1.0.0"

Once the package is restored, now we need to configure it. So open Startup.cs, add highlight line of code in ConfigureServices method.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);
    services.AddResponseCompression();
    services.AddMvc();
}

By Default, ASP.NET Core uses gzip compression. And now let’s add this middleware to HTTP pipeline, so add highlighted line in the Configure method of Startup.cs. Remember to add the Compression middleware before other middlewares which serves the files.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    app.UseApplicationInsightsRequestTelemetry();
    app.UseApplicationInsightsExceptionTelemetry();
    app.UseResponseCompression();
    app.UseMvc();
}

That’s it for setting it up. Now, let’s modify the default Get method of the Values Controller to return some large data.

[HttpGet]
public IEnumerable<string> Get()
{
    List<string> lstString = new List<string>();
    for (int i=1; i<= 100; i++)
    {
        lstString.Add("Value " + i.ToString());
    }
    return lstString;
}

Below two images show the difference in the response size. The first image is without compression, where the response size is 1.4 KB

Enable gzip compression in ASP.NET Core

And here is the output, after enabling the compression. The response size is reduced to 524 bytes. And you can also see the content-type is set to gzip in the response.

http://www.talkingdotnet.com/wp-content/uploads/2016/12/Enable-gZip-compression-in-ASP.NET-Core-1.png

You can also set the compression level to,

  • Optimal: The compression operation should be optimally compressed, even if the operation takes a longer time to complete.
  • Fastest: The compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed.
  • NoCompression: No compression should be performed on the file.
services.Configure<GzipCompressionProviderOptions>(options =>
          options.Level = System.IO.Compression.CompressionLevel.Optimal);
services.AddResponseCompression(options =>
{
    options.Providers.Add<GzipCompressionProvider>();
});

There are a couple of options available which can also be configured like enabling compression for HTTPS connection, compression to support different MIME types and using other compression providers. By default, compression is disabled for HTTPS requests. This can be enabled by setting the EnableForHttps flag to true.

services.AddResponseCompression(options =>
{
    options.EnableForHttps = true;
    options.Providers.Add<GzipCompressionProvider>();
});

You can also include other mime types (other than default) to include for compression. At the time of writing this post, default supported Mime Types are,

public static readonly IEnumerable<string> MimeTypes = new[]
{
    // General
    "text/plain",
    // Static files
    "text/css",
    "application/javascript",
    // MVC
    "text/html",
    "application/xml",
    "text/xml",
    "application/json",
    "text/json",
};

You can also include other mime type by concating other mime type in ResponseCompressionDefaults.MimeTypes. Like,

services.AddResponseCompression(options =>
{
   options.EnableForHttps = true;
   options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[]
                            {
                               "image/svg+xml",
                               "application/atom+xml"
                            });;
   options.Providers.Add<GzipCompressionProvider>();
});

That’s it.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

7 thoughts on “How to enable gzip compression in ASP.NET Core

  1. You must be a complete Windows guy to call gzip “gZip”. I’ve never seen anyone use capital Z for that.

    To the point, why is this in the ASP.NET level? I thought the web server was suppose to handle this.

    1. I didn’t realize about “gZip”. Thanks for pointing out and update the post. This middleware is there,if you would like ASP.NET to do your compression instead of a front-end web server.

Leave a Reply to Talking Dotnet Cancel reply

Your email address will not be published. Required fields are marked *

3 × 4 =