Response Compression in ASP.NET Core

What is Response Compression

Response compression is a technique that can be used to reduce the size of HTTP responses. It reduces the size of the HTTP response, which ultimately increases the responsiveness of the app.

Benefits of Response Compression

Pros of response compression are list below:

Improved performance:
Compressing the response can reduce the amount of data that needs to be transmitted over the network, leading to faster page load times and a better user experience.

Reduced bandwidth usage:
By compressing the response, you can reduce the amount of data that is transmitted over the network, which can lead to reduced bandwidth usage and lower costs for hosting and bandwidth

Better SEO:
Search engines take page load times into account when ranking websites, so a faster loading website may rank higher in search results.


How to Implement It

Configure it using .NET middleware with AddResponseCompression like this:

Response Compression in ASP.NET Core

Note: Setting EnableForHttps = true can expose requests to CRIME and BREACH attacks. To avoid this, use anti-forgery tokens when compressing data.


Compression Providers

.NET provides two compression providers:

  1. BrotliCompression
  2. GzipCompression

By default, .NET middleware uses the Brotli Compression Provider.

Response Compression in ASP.NET Core


Compression Levels

Each provider has the following compression levels:

  • Optimal: The compression operation should be optimally compressed, even if the operation takes a longer time to complete.
  • Fastest: The compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed.
  • No Compression: No compression will be performed on the file.
  • Smallest Size: The compression operation should create output as small as possible, even if the operation takes a longer time to complete.

This is how we can set compression level for our providers: Response Compression in ASP.NET Core


Custom Providers

Create a simple class and implement ICompressionProvider method and then you can add your CustomCompressionProvider like this in Program file Response Compression in ASP.NET Core

When Should We Compress, and When Not

  • Responses that are not natively compressed(e.g. CSS,JS,HTML,XML,JSON) are best candidates for compression

  • Don’t compress natively compressed assets (e.g. PNG) and smaller files (with 150-1000 bytes)


How Can I Verify That My Compression is Working

Add middleware, set compression levels, and send requests from Postman by setting different values of Accept-Encoding in the header.


MIME Types Supported by Default

  • text/css
  • text/xml
  • text/json
  • text/html
  • text/plain
  • application/xml
  • application/json
  • application/wasm
  • application/javascript

For more in depth knowledge you can read from Official Docs of Microsoft

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place