📜 ⬆️ ⬇️

How to use response compression in ASP.Net Core


Take advantage of the ASP.Net Core Response Compression Middleware component to reduce bandwidth requirements and increase the responsiveness of your applications.

ASP.Net Core is an open, cross-platform and modular environment for creating high-performance web applications. You can run ASP.Net Core applications on Windows, Linux and even MacOS. Fully modern web infrastructure, ASP.Net Core has built-in support for compressing responses, allowing you to reduce the size of responses and reduce response time. In this article, we will look at how response compression works and how we can use the intermediate processing component to compress responses in ASP.Net Core.

Compression is an easy way to reduce network traffic and increase the speed of data exchange between web server resources and clients. Gzip and Deflate are popular algorithms that are available to achieve compression, and most modern web browsers support response compression. Intermediate processing to compress ASP.Net Core responses by default uses Gzip compression.

Notice that the application pipeline in ASP.Net Core contains a number of request delegates that are called sequentially. We will use this to implement query compression. The intermediate processing pipeline is configured using the Configure method in the Startup.cs file. Here you can combine your ASP.Net Core pipelines. We will look at this further.
')

Creating an ASP.Net Core Application


Assuming that you are using Visual Studio Community Edition 2017 and .Net Core is already installed, follow these steps to create the first ASP.Net Core application in Visual Studio.

  1. In the Visual Studio IDE, click File -> New -> Project.
  2. Select "ASP.Net Core Web Application (.Net Core)" from the list of templates.
  3. Select Web Application.
  4. Enter the application name and click OK.
  5. In the "New ASP.NET Core Web Application" window - select ".Net Core and ASP.Net Core 2.0."
  6. Then select the “Web API” template, uncheck the “Enable Docker Support” checkbox, and click OK.

Now that you have created the ASP.Net Core project, install the ResponseCompression package through the NuGet package manager to enable Gzip compression in the newly created ASP.Net Core project.

 > Install-Package Microsoft.AspNetCore.ResponseCompression -Version 2.0.1 

At the time of this writing, the latest stable version of this package is 2.0.1.

Configure response compression in ASP.Net Core


After installing the ResponseCompression package, the next step is to configure it. To do this, you will need to edit the Startup.cs file and enable compression using the ConfigureServices and Configure methods. The ConfigureServices method is used to add services to the container, and the Configure method is used to configure the HTTP request pipeline. Note that both methods run automatically at run time.

Here's how to add a compression response in the ConfigureServices method of the Startup.cs file.

 public void ConfigureServices(IServiceCollection services) { services.AddResponseCompression(); services.AddMvc(); } 

The following code fragment illustrates the Configure method. Here we have added intermediate processing for compression to the pipeline before another processing.

 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseResponseCompression(); app.UseMvc(); } 

Implementing a controller in ASP.Net Core


Now that the response compression has been set in the project and configured, let's implement the controller to demonstrate the response compression. To do this, select the Controllers folder of your project and click "Add -> Controller". Select the API Controller - Empty template from the list of templates displayed in the Add Scaffold window, and enter a name for the controller when prompted. Then replace the default code with the following.

 [Produces(“application/json”)] [Route(“api/Default”)] public class DefaultController : Controller { // GET: api/Default [HttpGet] public List<Message> Get() { List<Message> lst = new List<Message>(); for(int index=0; index <100; index++) { Message message = new Message(); message.Text = “This is a text message.”; lst.Add(message); } return lst; } } 

The Message class contains only one string property.

 public class Message { public string Text { get; set; } } 

After performing this method, you can see that the size of the compressed response is 0.091 KB. When compression was turned off, the response size was 3.419 KB. To disable response compression, you can simply comment out the relevant lines in the Startup.cs file.

In the ConfigureServices method:

//services.AddResponseCompression();

In the Configure method:

//app.UseResponseCompression();

In order for the response compression to work, the client must inform the server about its capabilities by sending an Accept-Encoding header with the request. The server, in turn, must include this header in a compressed response in order to inform the client that the response has been compressed.

Please note that although compression of responses in ASP.Net Core is flexible, easy to configure and easy to use, it is comparatively slower than IIS compression. For more on compressing a response in ASP.Net Core, see the Microsoft ASP.NET Core documentation.

Source: https://habr.com/ru/post/349990/


All Articles