AddResponseCompression not working on .NET Core 3.1 controllers

Viewed 220

I am trying to get response compression working in a .NET Core 3.1, but it is not working.

In ConfigureServices I have the following;

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCompression();
    services.AddControllers();
    //All my other services
}

My Configure is the following:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }
    app.UseResponseCompression();
    app.UseHttpsRedirection()
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization()
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    })
}

When using a controller in Postman I am not seeing Content-Encoding set to gzip;

enter image description here

1 Answers

Found the solution, but forgot to post it:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
    services.AddResponseCompression(options =>
    {
        options.EnableForHttps = true;
        options.Providers.Add<GzipCompressionProvider>();
    });

    services.AddControllers();
    //All my other services
}
Related