Using Response Compression Middleware in ASP.NET Core

October 26, 2016 by Anuraj

C# ASP.NET Core Compression Middleware

This post is about using Response Compression Middleware in ASP.NET Core. Long back I wrote blog post on Enabling GZip Compression in ASP.NET5. It was a custom middleware I implemented in DNX days. Now as part of ASP.NET Core 1.1 Preview 1, Microsoft introduced Response Compression Middleware in ASP.NET Core.

To use Response Compression Middleware, first you need to add the Microsoft.AspNetCore.ResponseCompression package to your project.json file.

"Microsoft.NETCore.App": {
  "version": "1.1.0-preview1-001100-00",
  "type": "platform"
},
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.ResponseCompression" : "1.0.0-preview1-final"

You can enable compression using the Configure and ConfigureServices methods in the Startup.cs file.

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<GzipCompressionProviderOptions>
        (options => options.Level = CompressionLevel.Fastest);
    services.AddResponseCompression(options =>
    {
        options.Providers.Add<GzipCompressionProvider>();
    });
    
    services.AddMvc();
}

public void Configure(IApplicationBuilder app, 
    IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    app.UseResponseCompression();
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

Now you can restore the packages using dotnet restore command, and run the application using dotnet run command.

You can verify GZip compression enabled or not using Chrome developer tools. You can able to see something like this.

GZip encoding

You can find more features and details about ASP.NET Core 1.1 Preview 1 release here

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub