+977-9849845061

Enable Response Compression using GZIP in ASP.NET Core

ASP.NET Core comes with inbuilt Response Compression Middleware for compression, which by default uses GZIP Compression. GZIP is a technique, which is used for the file compression and decompression for faster network transfer.
Enable GZIP compression in ASP.NET Core
Install GZIP in our ASP.NET MVC Project, using NUGET Package Manager.
Step 1: Right click on ASP.NET Core and select Manage Nuget Package.

 Response Compression Middleware in ASP.NET Core by Nishan Aryal

Step 2: Browse Microsoft.AspNetCore.ResponseCompression.

Response Compression Middleware in ASP.NET Core by Nishan Aryal

Once ResponseCompression is installed, we need to configure it.

Let’s open Startup.cs file and add the code given below in ConfigurationService method.

Response Compression Middleware in ASP.NET Core by Nishan Aryal

Code snippet


 

By default, ASP.NET Core uses GZIP compression. Now, let’s add this middleware to HTTP pipeline, so add the line of code given below in Configure method of Startup.cs.

Remember to add Compression middleware before other middlewares, which serves the files.

Response Compression Middleware in ASP.NET Core by Nishan Aryal

Code snippet


 

So far we have installed and configured Response Compression in ASP.NET Core Application.
Creating dummy data for Test

Lets create a Test API to assess the data.

My TestAPI looks like, as shown below.

Response Compression Middleware in ASP.NET Core by Nishan Aryal
Code snippet

Lets run the Application and navigate to API.

In my case, it is http://localhost:51381/TestAPI/Get
Case I: When Response Compression is disabled.
(Comment below lines in Startup.cs)
  • services.AddResponseCompression(); (ConfigureServices method)
  • app.UseResponseCompression(); (Configure Method)
Response Compression Middleware in ASP.NET Core by Nishan Aryal
Case II: When Response Compression is enabled.
(UnComment below lines in Startup.cs)
  • services.AddResponseCompression(); (ConfigureServices method)
  • app.UseResponseCompression(); (Configure Method)

Response Compression Middleware in ASP.NET Core by Nishan Aryal

Comparision

 

Response Compression Disabled Response Compression Enabled
 Transferred data  6.4 KB  1.4 KB
 Finish time  32 ms  14 ms
 Load time  135ms  81 ms

Summary
So far we learned:

  • How to install Response Compression library in ASP.NET Core.
  • How to enable Response Compression as middle-ware.
  • How to transfer the data over the network by compressing.
  • Decrease Transfer and Load time of the data.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...