【问题标题】:AddResponseCompression not working on .NET Core 3.1 controllersAddResponseCompression 不适用于 .NET Core 3.1 控制器
【发布时间】:2021-06-08 17:20:56
【问题描述】:

我试图让响应压缩在 .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;

【问题讨论】:

  • @canton7 对,我删除了我的评论:)

标签: c# .net-core gzip asp.net-core-3.1


【解决方案1】:

找到了解决方法,但忘记发布了:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 2020-04-22
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2021-03-30
    • 1970-01-01
    相关资源
    最近更新 更多