AddResponseCompression 不适用于 .NET Core 3.1 控制器

AddResponseCompression not working on .NET Core 3.1 controllers

我试图让响应压缩在 .NET Core 3.1 中工作,但它不起作用。

ConfigureServices 我有以下内容;

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

我的Configure如下:

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();
    })
}

在 Postman 中使用控制器时,我没有看到 Content-Encoding 设置为 gzip;

找到解决方案,但忘记post它:

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
}