我正在尝试将Gzip中间件添加到我的ASP.net核心应用程序中.
我添加了以下包:
"Microsoft.AspNetCore.ResponseCompression":"1.0.0"
在我的startup.cs for Configure Services方法中,我有以下内容:
public void ConfigureServices(IServiceCollection services) { services.Configure(options => options.Level = CompressionLevel.Fastest); services.AddResponseCompression(options => { options.Providers.Add (); }); services.AddMvc(); }
在我的Configure方法中,我有以下内容:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseResponseCompression(); app.UseMvc(); }
但是,当我尝试加载页面时,它不会像压缩Gzip那样通过.我已经使用了字符串响应和输出视图.chrome中的响应标头如下所示:
我在视觉工作室开发的Windows机器上.运行应用程序时,我尝试从Visual Studio(通过F5)运行,并从命令行使用"dotnet run"命令.既没有输出GZip压缩.
在.NET芯启用GZIP 2. *
1安装Microsoft.AspNetCore.ResponseCompression
用Install-Package Microsoft.AspNetCore.ResponseCompression
的命令或NuGet包管理器。
2.将以下代码添加到Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseResponseCompression(); app.UseMvc(); } public void ConfigureServices(IServiceCollection services) { // Configure Compression level services.Configure(options => options.Level = CompressionLevel.Fastest); // Add Response compression services services.AddResponseCompression(options => { options.Providers.Add (); options.EnableForHttps = true; }); }
通过添加响应压缩选项属性“ EnableForHttps”来解决此问题,如下所示:
services.AddResponseCompression(opt => { opt.Providers.Add(); opt.EnableForHttps = true; }); services.Configure (options => options.Level = CompressionLevel.Fastest);