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
- Server Response: If compression is enabled, the response is compressed using the best available format (e.g., Brotli or Gzip).
- Client Decompression: The browser automatically decompresses the response and renders it.
Testing Response Compression
Method 1. Browser DevTools (Chrome, Edge, Firefox)
- Open Developer Tools (F12 or Ctrl + Shift + I).
- Go to the Network tab.
- 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

Join the conversation! Your thoughts help the community grow.