.net 核心中的 GZIP 不起作用 [英] GZIP in .net core not working
问题描述
我正在尝试将 Gzip 中间件添加到我的 ASP.net 核心应用程序中.
I'm attempting to add Gzip middleware to my ASP.net core app.
我添加了以下包:
"Microsoft.AspNetCore.ResponseCompression": "1.0.0"
"Microsoft.AspNetCore.ResponseCompression": "1.0.0"
在我的配置服务方法的startup.cs中,我有以下内容:
In my startup.cs for the Configure Services method I have the following :
public void ConfigureServices(IServiceCollection services)
{
services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
});
services.AddMvc();
}
在我的配置方法中,我有以下内容:
In my Configure method I have the following :
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseResponseCompression();
app.UseMvc();
}
但是,当我尝试加载页面时,它不会以 Gzip 压缩格式出现.我使用了字符串响应和输出视图.chrome 中的响应标头如下所示:
However when I try and load a page, it doesn't come through as Gzip compressed. I have used both a string response and outputting a view. The response headers in chrome look like :
我在 Visual Studio 中开发的 Windows 机器上.运行应用程序时,我尝试从 Visual Studio(通过 F5)运行,并从命令行使用dotnet run"命令.既不输出 GZip 压缩.
I am on a windows machine developing in visual studio. When running the app I have tried just running from Visual Studio (Via F5), and also using the "dotnet run" command from command line. Neither output GZip compression.
推荐答案
在.net core 2中启用GZIP.*
1. 使用 Install-Package Microsoft.AspNetCore.ResponseCompression
命令或 nuget 包管理器安装 Microsoft.AspNetCore.ResponseCompression
.
2.在Startup.cs
To Enable GZIP in .net core 2.*
1. Install Microsoft.AspNetCore.ResponseCompression
with Install-Package Microsoft.AspNetCore.ResponseCompression
command or nuget package manager.
2. Add the following code into Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseResponseCompression();
app.UseMvc();
}
public void ConfigureServices(IServiceCollection services)
{
// Configure Compression level
services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
// Add Response compression services
services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
}
这篇关于.net 核心中的 GZIP 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!