问题描述
context.Services.AddResponseCompression(o =>
{
o.EnableForHttps = true;
});
// We use brotli by default : https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.1
//services.Configure<GzipCompressionProviderOptions>(o => o.Level = System.IO.Compression.CompressionLevel.Optimal);
context.Services.Configure<brotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
和
app.UseResponseCompression();
当我在浏览器中签入时,似乎压缩已激活:
现在,我测试网站的速度,第一个建议是添加文本压缩。 所以,我不明白为什么会有这样的消息:
有人对这个问题有任何想法吗?
解决方法
The ASP.NET Core docs概述,使用CompressionLevel.Fastest
将导致压缩完成最快,而不是网页加载最快。
要获得最高级别的压缩,您应该使用CompressionLevel.Optimal
。
我找到了问题,希望它可以对其他人有所帮助。必须在UseStaticFiles之前放置对UseResponseCompression的调用,这样它也将考虑所有静态(css,js)文件。
// Must be before UseStaticFiles to compress static
//files and UseMvc to compress MVC responses
app.UseResponseCompression();
app.UseStaticFiles();