[英]How to enable compression in aspnetboilerplate dynamic web api
I tried adding Microsoft.AspNetCore.ResponseCompression in Myproject.web.host And configured this我尝试在 Myproject.web.host 中添加 Microsoft.AspNetCore.ResponseCompression 并配置它
public void ConfigureServices(IServiceCollection services)
{
//other configs...
services.AddResponseCompression();
//other configs...
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//other configs...
app.UseResponseCompression();
app.UseMvc();
}
it worked for swagger, but not for the generated dynamic web api它适用于 swagger,但不适用于生成的动态 web api
Try to use a tool such as F12 developer tools, Fiddler or Postman to check the Accept-Encoding setting in the request header and the response headers.尝试使用 F12 开发人员工具、Fiddler 或 Postman 等工具检查请求 header 和响应标头中的 Accept-Encoding 设置。 Perhaps the Content-Encoding and Vary headers aren't present on the response.
响应中可能不存在 Content-Encoding 和 Vary 标头。
To solve this issue, you could try to refer the following steps to set the compression provider:要解决此问题,您可以尝试参考以下步骤来设置压缩提供程序:
create a BrotliCompressionProvider class:创建一个 BrotliCompressionProvider class:
public class BrotliCompressionProvider : ICompressionProvider
{
public string EncodingName => "br";
public bool SupportsFlush => true;
public Stream CreateStream(Stream outputStream) => new BrotliStream(outputStream, CompressionMode.Compress);
}
Change the configure service as below:更改配置服务如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddResponseCompression(options => {
options.Providers.Add<BrotliCompressionProvider>();
options.EnableForHttps = true;
});
services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//other configs...
app.UseResponseCompression();
app.UseMvc();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.