Enhancing Performance with Response Compression in .NET Core

Introduction

Performance optimization is critical to delivering a fast and seamless user experience in modern web applications. Enabling response compression in .NET Core is one of the simplest yet effective ways to reduce bandwidth usage and improve API response times.

By using AddResponseCompression(), you can significantly decrease the size of responses sent to the client, resulting in faster load times and reduced network latency.

What is Response Compression?

Response compression is a technique that reduces the size of HTTP responses using algorithms like Gzip and Brotli. It is especially useful for text-based content such as,

  • JSON & XML (API responses)
  • HTML, CSS, and JavaScript
  • Plain text and CSV files

Without Compression

yaml

  • Response Size: 100 KB
  • Time Taken: 1.2 seconds

With Compression (Gzip or Brotli)

yaml

  • Response Size: 30 KB
  • Time Taken: 0.5 seconds

Up to 70% reduction in size.

How do you enable response compression in .NET Core?

Step 1. Install the Required Package.

If you are using an older .NET Core version (<3.1), install the package.

dotnet add package Microsoft.AspNetCore.ResponseCompression

For .NET 6 and later, this is included by default, so there is no need to install it separately.

Step 2. Configure Response Compression in the Program.cs.

Modify your Program.cs file to enable compression.

var builder = WebApplication.CreateBuilder(args);

// Enable Response Compression
builder.Services.AddResponseCompression(options =>
{
    options.EnableForHttps = true; // Enable compression for HTTPS requests
    options.Providers.Add<BrotliCompressionProvider>(); // Add Brotli compression
    options.Providers.Add<GzipCompressionProvider>(); // Add Gzip compression
});

// Configure compression levels (Optional)
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
    options.Level = System.IO.Compression.CompressionLevel.Fastest;
});

builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
    options.Level = System.IO.Compression.CompressionLevel.Optimal;
});

var app = builder.Build();

// Enable Middleware for Compression
app.UseResponseCompression();

app.MapControllers();
app.Run();

How does it work?

Client Request: The browser sends a request with an Accept-Encoding header, specifying the supported compression formats.

Accept-Encoding: gzip, br

  1. Server Response: If compression is enabled, the response is compressed using the best available format (e.g., Brotli or Gzip).
  2. Client Decompression: The browser automatically decompresses the response and renders it.

Testing Response Compression

Method 1. Browser DevTools (Chrome, Edge, Firefox)

  1. Open Developer Tools (F12 or Ctrl + Shift + I).
  2. Go to the Network tab.
  3. Select an API request and check the Response Headers:

With Compression

  • content-encoding: gzip
  • content-length: 15,000

Without Compression

  • content-length: 50,000

Method 2. Using Postman

  1. Send a GET request to your API.
  2. Check the response headers for content-encoding: gzip.

Method 3. Using cURL (Command Line)

Run the following command.

curl -H "Accept-Encoding: gzip" -I https://localhost:5001

If compression is working, the response will contain.

content-encoding: gzip

Choosing the Right Compression Algorithm
 

Compression Type Compression Ratio Speed (Fastest → Slowest) Best For
Brotli (br) Best Slower than Gzip HTML, JSON, APIs
Gzip (gzip) High Faster than Brotli API responses, text files
Deflate (deflate) Medium Very Fast Legacy clients

Brotli is recommended for modern browsers, while Gzip is widely supported across all platforms.

When to Use Response Compression?

  • APIs returning large JSON responses, Serving static files (HTML, CSS, JS)
  • Reducing bandwidth usage on slow connections
  • Improving performance on mobile networks

Not Recommended for,

  • Already compressed content (e.g., images, PDFs, videos)
  • Highly dynamic content (e.g., real-time stock prices)

Common Issues & Fixes
 

Issue Cause Fix
No content-encoding header Compression middleware not enabled Add app.UseResponseCompression()
Compression not working over HTTPS EnableForHttps not set options.EnableForHttps = true;
API response is not compressed The response is too small Ensure response size > 150 bytes
Already compressed content Trying to compress image/png, application/zip Exclude non-text MIME types


Conclusion

  • Response compression in .NET Core improves performance by reducing response sizes.
  • Enable Brotli & Gzip for better compression efficiency.
  • Use app.UseResponseCompression() to apply compression at runtime.
  • Test using DevTools, Postman, or cURL.

Final Tip. If you serve large static files, also enable Static File Compression in app.UseStaticFiles()

Ebook Download
View all
Learn
View all