ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Response Compression Middleware in ASP.NET Core

.Net .Net Core about 8 years ago || 4/4/2018 || 1.7 K View

  • Hide

In this article, we will learn how to compress response in middleware in Asp.Net Core. Asp.Net Core has inbuilt Response Compression Middleware for compression which by default uses GZIP compression. GZIP is a technique which used for the file compression and decomposition for faster network transfer.

What is Response Compression?

Normally, any reaction not locally compressed can profit by reaction compression. Reactions not locally compressed commonly include: CSS, JavaScript, HTML, XML, and JSON. You shouldn't pack locally compressed resources, for example, PNG documents. On the off chance that you Endeavor to further pack a locally compressed reaction, any little extra lessening in size and transmission time will probably be dominated when it took to prepare the compression. You additionally shouldn't pack records littler than around 150-1000 bytes (relies on upon the document's content and the productivity of compression), as doing as such may deliver a compressed document bigger than the record itself.

At the point when a customer can prepare compressed content, the customer must advise the server of its abilities by sending the Accept-Encoding header with the demand. At the point when a server sends compressed content, it must incorporate data in the Content-Encoding header on how the compressed reaction is encoded. Content encoding designations are appeared underneath demonstrating which ones are upheld by the middleware.

When to use Response Compression Middleware

Utilize Response Compression Middleware when we can't utilize the Dynamic Compression module in IIS, the Apache mod_deflate module, NGINX Compression, and Decompression, or your application is facilitated straightforwardly on WebListener server or Kestrel. The main motivation to utilize the server-based reaction compression advances in IIS, Apache, or Nginx is that the execution of the middleware likely won't coordinate that of the server modules.

Now let’s demonstrate by creating an application.

Creating Application

  • Open Visual Studio (In my case I’m using Visual Studio update 3)
  • File > New Project.
  • Choose .Net Core Web Application
  • Provide a suitable name for the project.
  • Click on OK.

  • Now choose Web Application from the template and click on Ok.

  • It will take a few seconds to load our project.

Package Installation

Now the first thing we will do here is to install the package that we need for the compression. To do that:

  • Right click on the project in the Solution Explorer.
  • Click on Manage NuGet Packages
  • Search for ‘Microsoft.Asp.NetCore.ResponseCompression’

Install the package then it will automatically be enlisted in the project.json file.

Now let’s add the service in the Startup class in the ConfigureServices method as shown in the code snippet below:

As we know now that by default, Asp.Net Core uses GZIP compression, so now let’s add this middleware to HTTP pipeline. Add the following code in the configure method of Startup class as shown in the code snippet below:

Creating WebAPI:

  • Now let’s create an API Controller to access the data.
  • Right Click on the controller folder >> Add >> New Item.
  • Choose Web API Controller class from the template.

  • Provide name as ‘CheckController’ or any other.
  • And Click on Add.

Now let’s add the code in it as shown in the code snippet below:

Now let’s run the application and navigate to CheckAPI/Get, we need to get the following output:

Now press F12 and navigate to Network and press f5 to reload the application, we can see the transfer rate and time it took to load the data:

Now let’s stop the project and comment on the lines that we added in Startup class i.e ‘services.AddResponseCompression from the ConfigureServices method and ‘app.UseResponseCompression’ from Configure method. And the application again and navigate to the Get function of API Controller.

Press F12 and navigate to Network and see the transfer rate and time. It must be higher than earlier.

As we can see the time it took was higher when we did not use the compression.

  • 3
  • 1
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Prakash Pokhrel

Prakash Pokhrel

https://np.linkedin.com/in/prakash-pokhrel-42a699a2

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey