Compression is an easy and effective way to reduce the size and increase the speed of communication between a client and remote resource. Two common compression algorithms used on the web are GZip and Deflate. The Accept-Encoding header is used by a client to restrict the encoding types that are acceptable in the response.
Here is the Compression middleware implementation, which whether GZip compression supported by the “Accept-Encoding” header value. If it supports, middleware compress the body and send the compressed stream to the client. Since browser doesn’t know about the content encoding, you need to set the content encoding header value as well.
Here is the middleware implementation.
And here is the helper class, which helps to inject the middleware to the HTTP request pipeline.
And you can use this middleware in the Configue method in Startup.cs file, like this
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.
Did you like this article? Share it with your friends