.netcore 应用程序上的 ResponseCompression 运行 TargetFramework net472

ResponseCompression on .netcore app running TargetFramework net472

我一直在尝试让 Microsoft.AspNetCore.ResponseCompression 2.1.1 在以 .net 完整框架 472 为目标的 .net 核心项目应用程序上工作,但无济于事。

我已将项目剥离到核心部分,只留下:

        public void ConfigureServices(IServiceCollection services)
        { 
            services.AddResponseCompression(options =>
            {
                options.EnableForHttps = true;

            });
        } 


        public void Configure(IApplicationBuilder app)
        { 
            app.UseResponseCompression();   

            app.UseStaticFiles(); 
        }

我正在尝试让中间件压缩静态文件。

请求headers:

GET http://localhost:3721/css/Site.css HTTP/1.1
Host: localhost:3721
Connection: keep-alive
Accept: text/css
Accept-Encoding: gzip, deflate, br, *

响应headers:

HTTP/1.1 200 OK
Date: Sat, 03 Nov 2018 01:20:21 GMT
Content-Type: text/css
Server: Kestrel
Last-Modified: Thu, 13 Jul 2017 16:35:14 GMT
Accept-Ranges: bytes
ETag: "1d2fbf600bbcfda"
Vary: Accept-Encoding
Content-Length: 730

我的实现是否有任何明显的错误会导致静态文件响应 headers 到 not echo:

content-encoding: gzip (or other)

是否可以hook到决定是否压缩请求的方法中?

对于您的回复 headers,它已经被压缩并用 Vary: Accept-Encoding 表示 cache (vary) responses

Vary

When sent by the server with a value of Accept-Encoding to clients and proxies, the Vary header indicates to the client or proxy that it should cache (vary) responses based on the value of the Accept-Encoding header of the request. The result of returning content with the Vary: Accept-Encoding header is that both compressed and uncompressed responses are cached separately.

您可以尝试以下不同的方式来查看不同的结果:

  • Chrome:按Ctrl + F5检查响应headers。
  • 试试 PostMan
  • 试试 Fiddler