.net核心中的GZIP无法正常工作 [英] GZIP in .net core not working

查看:125
本文介绍了.net核心中的GZIP无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将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中,使用Configure Services方法时,我有以下内容:

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();
}

在我的Configure方法中,我有以下内容:

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 :

我在Windows计算机上在Visual Studio中开发。在运行该应用程序时,我尝试仅从Visual Studio(Via 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆