Enable Response Compression in ASP.NET Core in 5 Easy Steps
It is a new open-source and cross-platform framework for creating modern cloud based internet connected applications, such as web apps, IoT apps and mobile backends. ASP.NET Core apps run on .NET Core or on the full .NET Framework. It was created to provide an optimized development framework for apps that are deployed to the cloud or run on-premises.
The first preview release of ASP.NET was done about 15 years ago as part of the .NET Framework. Since then millions of developers have used it to build and run great web apps, and over the years we have added and evolved many capabilities to it.
ASP.NET Core has a number of architectural changes that yields in a much leaner and modular framework. ASP.NET Core is no longer based on System.Web.dll. It is based on a set of granular and well factored NuGet packages.
As an ASP.NET developer you must be aware that IIS comes with its own compression module that can well compress the response content. This can dramatically lower the network bytes giving good performance benefits. However, what if you aren’t using IIS at all? ASP.NET Core gives you a middleware which 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
Develop by creating a new ASP.NET Core web application. You can use the Web Application template so that the basic start-up configuration is already made for you.
2. Add ASP.NET Core Response Compression NuGet package
Once the web application is developed you need to add a NuGet package that performs the compression for you. To do that, press 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 see this entry :
The Microsoft.AspNetCore.ResponseCompression package is what you need to make the response compression. Press the Install button and install the package.
3. Add the Response Compression service to the application
Next, open the Startup.cs file and modify the ConfigureServices() method as shown below :
public void ConfigureServices(IServiceCollection services)
(options => options.Level = CompressionLevel.Optimal);
Here, you first do the GzipCompressionProviderOptions such 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)
Note that the UseResponseCompression() method that sums up the middleware is called before any of the other UseXXXXX() methods. This way this middleware gets a chance to compress the response.
5. Verify that the response is compressed
This completes the entire process. Run the application and observe the response in F12 tools You will find that the Content-Encoding header is set to gzip as expected.
We conclude for now. Keep coding!!
Let us know your opinion in the comments sections below. And feel free to refer Microsoft’s site to gather more information.
If you want to improve your skill in ASP.Net and excel yourself in ASP.NET training program; our institute, CRB Tech Solutions would be of great help and for you. Come and join us with our well structured program for ASP .Net.
Stay connected to CRB Tech for more technical optimization and other updates and information.