0

I tried adding Microsoft.AspNetCore.ResponseCompression in Myproject.web.host And configured this

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

1 Answers1

0

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. Perhaps the Content-Encoding and Vary headers aren't present on the response.

To solve this issue, you could try to refer the following steps to set the compression provider:

create a 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();
 }
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30