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:
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:
- BrotliCompression
- 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
Whenever you're ready, there are 3 ways I can help you:
- Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
- Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
- Patreon community : Get access to all of my blogs and articles at one place