| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
Response Compression in ASP.NET Core
Sep 2, 2023
3 min read

Response Compression in ASP.NET Core

Sponsor this Newsletter

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:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddResponseCompression(options =>
{
    options.EnableForHttps = true;
});
var app = builder.Build();
app.UseResponseCompression();
app.MapGet("/", () => "Hello World!");
app.Run();

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.

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddResponseCompression(options =>
{
    options.EnableForHttps = true;
    options.Providers.Add<BrotliCompressionProvider>();
    options.Providers.Add<GzipCompressionProvider>();
    options.MimeTypes = ResponseCompressionDefaults.MimeTypes;
});

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:

builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
    options.Level = CompressionLevel.SmallestSize;
});
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
    options.Level = CompressionLevel.Optimal;
});

Custom Providers

Create a simple class and implement ICompressionProvider method and then you can add your CustomCompressionProvider like this in Program file

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddResponseCompression(options =>
{
    options.Providers.Add<BrotliCompressionProvider>();
    options.Providers.Add<GzipCompressionProvider>();
    options.Providers.Add<CustomCompressionProvider>();
});
var app = builder.Build();
app.UseResponseCompression();
app.Run();

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 Sep 2, 2023 .

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
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

© 2025 Muhammad Waseem. All rights reserved.