Meditation and Mindfulness for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...

Enable Response Compression in ASP.NET Core in 5 Easy Steps

As an ASP.NET developer you are probably aware that IIS comes with its own compression module that can effectively compress the response content. This can dramatically reduce the network bytes giving good performance benefits. However, what if you aren't using IIS at all? Luckily, ASP.NET Core offers you a middleware that can compress the response content for you. In this article you will learn to configure the ASP.NET Core response compression middleware in your web application.

1. Create a new ASP.NET Core web application

Begin by creating a new ASP.NET Core web application. You can use the Web Application template so that the basic startup configuration is already done for you.

2. Add ASP.NET Core Response Compression NuGet package

Once the web application is created you need to add a NuGet package that does the compression for you. To do that right click on the References folder and pick Manage NuGet packages shortcut menu option. This will open the NuGet packages tab. Search for Response Compression and you will se this entry :

 

The Microsoft.AspNetCore.ResponseCompression package is what you need to enable the response compression. Click on the Install button and install the package. 

3. Add the Response Compression service to your application 

Next, open the Startup.cs file and modify the ConfigureServices() method as shown below :

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

Here, you first configure the GzipCompressionProviderOptions so that highest level of compression is used. Then you ad the response compression service using the AddResponseCompression() method. While calling the AddResponseCompression() method you specify Gzip compression in the options. 

4. Add the Response Compression middleware to the request pipeline

Then go to the Configure() method and add the response compression middleware to the request pipeline.

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

Note that the UseResponseCompression() method that adds the middleware is being called before any of the other UseXXXXX() methods. This way the middleware gets chance to compress the response.

5. Verify that the response is being compressed

This completes the configuration. Run the application and observe the response in F12 tools (or equivalent). You will find that the Content-Encoding header is set to gzip as expected.

For more information about response compression go here.

That's it for now! Keep coding!!




Bipin Joshi is a software consultant, an author and a yoga mentor having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 28 Feb 2017



Tags : ASP.NET ASP.NET Core MVC .NET Framework