How to use Brotli for response compression in ASP.NET Core
By FoxLearn 1/4/2025 4:09:50 AM 157
Since CPU resources are more readily available compared to limited network bandwidth, implementing response compression middleware in ASP.NET Core helps optimize application performance.
Brotli is a newer compression algorithm for .NET Core that offers better compression results than Gzip or Deflate. It's also supported by most modern web browsers, including Google Chrome, Mozilla Firefox, Opera, and Microsoft Edge.
Getting started with Brotli in ASP.NET Core
Start by creating a new ASP.Net Core project in Visual Studio.
Follow these steps to create an ASP.Net Core application in Visual Studio:
- In Visual Studio, go to File > New > Project.
- Choose ASP.Net Core Web Application (.Net Core) from the available templates.
- Enter a name for your project, then click OK.
- In the New .Net Core Web Application window, select API.
- Choose ASP.NET Core as the version for the runtime.
- Uncheck Enable Docker Support, Configure for HTTPS, and No Authentication, as these features are not required.
- Click OK.
This will create a new ASP.Net Core project with a default Controller to help you build and run RESTful HTTP services.
Implementing a response compression provider in ASP.NET Core
The client uses the Accept-Encoding header to specify preferred encoding types for the response. ASP.Net Core’s response compression middleware checks this header to determine if a compression method can be used. By default, Gzip is used, but it can be overridden with custom middleware.
To use Brotli in your ASP.Net Core project, first install the Brotli.Net NuGet package. Then, create a class called CustomCompressionProvider that extends the ICompressionProvider interface and implements the CreateStream method.
public class CustomCompressionProvider : ICompressionProvider { public string EncodingName => "br"; // Brotli encoding public bool SupportsFlush => true; // Indicates support for flush public Stream CreateStream(Stream outputStream) { return new BrotliStream(outputStream, CompressionLevel.Fastest, false); } }
In this example, CompressionLevel.Fastest is used to prioritize speed. Other available options include CompressionLevel.NoCompression (which disables compression) and CompressionLevel.Optimal (which provides the highest compression).
Configuring response compression in ASP.NET Core
To configure Brotli in your ASP.Net Core application, you need to modify the Startup class, which contains the Configure and ConfigureServices methods.
public void Configure(IApplicationBuilder app) { app.UseResponseCompression(); // Enable response compression app.UseStaticFiles(); // Serve static files app.UseMvcWithDefaultRoute(); // Set up default routing }
Next, configure the response compression middleware by adding your custom provider:
services.AddResponseCompression(options => { options.Providers.Add(new CustomCompressionProvider()); // Use custom Brotli provider });
Below is the complete code for the Startup class, incorporating the above configurations.
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddResponseCompression(options => { options.Providers.Add(new CustomCompressionProvider()); }); } public void Configure(IApplicationBuilder app) { app.UseResponseCompression(); app.UseStaticFiles(); app.UseMvcWithDefaultRoute(); } }
Once you've set up the configuration, your ASP.NET Core Web API services will return compressed responses.
The response compression middleware in ASP.NET Core allows you to use custom compression algorithms like Brotli, which offers about 20% better compression than Gzip and is approximately 20 times faster at decompressing content.
- How to use TinyIoC in ASP.NET Core
- How to use FusionCache in ASP.NET Core
- How to use SignalR in ASP.NET Core
- How to use the Dapper ORM in ASP.NET Core
- How to enable CORS in ASP.NET Core
- How to implement HTTP.sys web server in ASP.NET Core
- How to use File Providers in ASP.NET Core
- How to use policy-based authorization in ASP.NET Core