# ASP.NET Core: enable GZIP, Deflate and Brotli response compression

> It can be helpful to withstand the compression of responses when they are large, which can greatly reduce the time taken to download them. Activate gzip…

- Author: Jérôme Giacomini
- Language: en
- Canonical URL: [ASP.NET Core: enable GZIP, Deflate and Brotli response compression](https://jeromegiacomini.net/articles/2018/09/25/asp-net-core-enable-gzip-deflate-and-brotli-response-compression)
- Published: 2018-09-25
- Last modified: 2026-09-05
- Topics: ASP.NET Core, .NET, C#

It can be helpful to withstand the compression of responses when they are large, which can greatly reduce the time taken to download them.

## Activate gzip compression

To enable gzip compression is very simple the ASP.NET Core framework provides ready-made classes.

In the Startup file, simply enable the response compression as follows:

```csharp

using Microsoft.AspNetCore.ResponseCompression;

...

public ConfigureServices(IServiceCollection services)
{
     services.AddResponseCompression();
}

```


Note that you can also configure the Gzip compression level as follows:

```csharp

services.Configure<GzipCompressionProviderOptions>(o => o.Level = System.IO.Compression.CompressionLevel.Optimal);

```


If your site is in https you can enable compression as follows:

```csharp

services.AddResponseCompression(o =>
{
     o.EnableForHttps = true;
});

```


Then in the Configure method you must call the following extension method:

```csharp

app.UseResponseCompression();

```


## Activate the Brotli and Deflate compression

ASP.NET Core does not provide **compression provider** for both formats.

So we have to write them down anyway.

**Compression provider Brotli:**

```csharp

public class BrotliCompressionProvider : ICompressionProvider
{
    private readonly CompressionLevel _compressionLevel;
    public BrotliCompressionProvider(CompressionLevel compressionLevel = CompressionLevel.Fastest)
    {
        _compressionLevel = compressionLevel;
    }

    public string EncodingName => "br";
    public bool SupportsFlush => true;
    public Stream CreateStream(Stream outputStream)
    {
       return new BrotliStream(outputStream, _compressionLevel);
    }
}

```


## Activate the deflate compression

**Compression provider Default:**

```csharp

public class DeflateCompressionProvider : ICompressionProvider
{
    private readonly CompressionLevel _compressionLevel;
    public DeflateCompressionProvider(CompressionLevel compressionLevel = CompressionLevel.Fastest)
    {
        _compressionLevel = compressionLevel;
    }

    public string EncodingName => "deflate";
    public bool SupportsFlush => true;
    public Stream CreateStream(Stream outputStream)
    {
       return new DeflateStream(outputStream, _compressionLevel);
    }
}

```


**Note that the DeflateStream class does not meet the Deflate standard well so if you use a client other than .NET it will not be able to decode your file.**

Then the AddResponseCompression method must be modified as follows:

```csharp

services.AddResponseCompression(o =>;
{
   o.Providers.Add(new BrotliCompressionProvider());
   o.Providers.Add(new DeflateCompressionProvider());
   o.EnableForHttps = true;
});

```


## Recovering a Compressed Answer

Now that we have a compressed answer there is no need to use it.

To have compressed content the client must send the Accept-Encoding header with gzip, or deflate, or br value.

It can also send the 3 gzip,deflate,br” to tell the server that it supports the 3 formats.

But customers do not automatically decompress the answers:

Thankfully my library [Tiny. RestClient](https://github.com/jgiacomini/Tiny.RestClient) automatically does.

tt The HttpClient API class enables automatic decompression of compressed content.

But its use is not obvious because it has to be handled specifically:

```csharp

HttpClientHandler handler = new HttpClientHandler()
{
   AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};


using (var client = new HttpClient(handler))
{
    ....
}

```


**Watch out for it.’At the moment the compression broke n’is not yet managed by HttpClient API.**

## When do you need to activate the compression?

The documentation specifies that files less than 1000 bytes should not be compressed as this can produce a compressed file larger than the uncompressed file.

It is also worth noting that compression adds to the server-side computing load.

Happy coding.

## To go further
- [Official documentation of ASP.NET Core](https://docs.microsoft.com/fr-fr/aspnet/core/performance/response-compression?view=aspnetcore-2.1)
- [Tiny.RestClient: A REST client that automatically manages compression / decompression](https://github.com/jgiacomini/Tiny.RestClient)
