gpt4 book ai didi

c# - AddResponseCompression 不适用于 .NET Core 3.1 Controller

转载 作者:行者123 更新时间:2023-12-04 17:18:19 26 4
gpt4 key购买 nike

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

ConfigureServices 中,我有以下内容;

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

我的配置如下:

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 中使用 Controller 时,我没有看到 Content-Encoding 设置为 gzip;

enter image description here

最佳答案

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

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
}

关于c# - AddResponseCompression 不适用于 .NET Core 3.1 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67891755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com