如何使用响应压缩在ASP。网络核心

利用响应压缩中间件在ASP。网络核心to reduce bandwidth requirements and improve the responsiveness of your apps.

  • 在Facebook上分享
  • 在Twitter上分享
  • 分享在LinkedIn
  • 在Reddit分享
  • 通过电子邮件分享
  • 印刷资源
汽车速度表
思想库

作为一个彻底的现代web框架,ASP。网络核心has built-in support for response compression, allowing you to reduce the size of responses and shorten response times. In this article we will examine how response compression works and how we can take advantage of response compression middleware in ASP.NET Core.

压缩是一种简单的方法来减少网络流量和增加的速度web服务器之间的通信资源和客户或消费者。流行的压缩算法可以实现这一目标包括Brotli, Gzip,缩小,大多数现代web浏览器都支持响应压缩。ASP。网络核心的响应压缩中间件使用Brotli或Gzip压缩在默认情况下,这取决于支持客户端。

我们将使用中间件实现响应压缩。注意,如果你的ASP。网络核心application is being deployed on an IIS, Apache, or Nginx web server, you should instead使用内置的web服务器提供的压缩为更好的性能。使用响应压缩中间件只有如果你无法使用web服务器的压缩,或如果你的应用是托管在一个web服务器缺乏内置的压缩,如红隼。

请注意,在ASP应用程序的管道。网络核心contains a series of request delegates that are invoked sequentially. We will take advantage of one of these middleware components to implement request compression. The middleware pipeline is configured in the Program.cs file. This is where you can chain together your ASP.NET Core pipeline. We will discuss this further in the sections that follow.

创建一个ASP。网络核心minimal Web API project in Visual Studio 2022

首先,让我们创建一个ASP。网络核心7 minimal Web API project in Visual Studio 2022. Follow these steps:

  1. 启动Visual Studio 2022 IDE。
  2. 点击“创建新项目。”
  3. 在“创建新项目”窗口中,选择“ASP。网络核心Web API” from the list of templates displayed.
  4. 单击Next。
  5. 在“配置您的新项目”窗口中,指定新项目的名称和位置。
  6. (可选)检查“解决方案和项目在同一个目录”复选框,根据您的喜好。
  7. 单击Next。
  8. 接下来,所示的“其他信息”窗口中选择“网络7.0(当前)”作为框架和取消选中该复选框,表示“使用控制器…”在这个例子中我们将使用最小的api。离开“验证类型”设置为“无”(默认)。
  9. 确保复选框“使码头工人”,“为HTTPS,配置”和“使开放的API支持”都不。我们不会使用这些特性。
  10. 单击Create。

接下来,安装微软的ResponseCompression包通过ASP NuGet包管理器。网络核心minimal Web API project you just created. As of this writing, the latest stable version of the ResponseCompression package is 2.2.0.

>安装包Microsoft.AspNetCore。ResponseCompression - version 2.2.0

我们将使用ASP。网络核心7 minimal Web API project to implement response compression in the sections below.

配置响应压缩在ASP。网络核心

您可以启用响应压缩程序。cs文件使用以下代码。

builder.Services.AddResponseCompression ();

上面的代码片段将响应压缩服务添加到容器中。如果你想使响应压缩HTTPS,你应该输入以下代码的程序。cs文件:

builder.Services。AddResponseCompression(选项= >{选项。EnableForHttps = true;});

添加响应压缩中间件请求处理管道,为了压缩动态响应,您应该使用以下配置HTTP请求管道段代码。

app.UseResponseCompression ();

配置压缩供应商为获得最佳性能

请注意,一旦你添加了一个压缩供应商,没有其他压缩提供者将自动被添加。你需要根据需要编写自己的代码添加它们。

builder.Services。AddResponseCompression(选项= >{选项。EnableForHttps = true;options.Providers.Add < BrotliCompressionProvider > ();options.Providers.Add < GzipCompressionProvider > ();});

您可以配置Brotli和Gzip压缩供应商为获得最佳性能(意味着更快的压缩,但更大的有效载荷),如下面代码片段所示。

builder.Services。配置< BrotliCompressionProviderOptions >(选项= >{选项。水平= CompressionLevel.Fastest;});builder.Services。配置< GzipCompressionProviderOptions >(选项= >{选项。水平= CompressionLevel.Optimal;});

程序的完整源代码。cs文件

这是程序的完整源代码。cs文件供你参考。

使用Microsoft.AspNetCore.ResponseCompression;使用System.IO.Compression;var builder = WebApplication.CreateBuilder (args);/ /服务添加到容器中。builder.Services。AddResponseCompression(选项= >{选项。EnableForHttps = true;options.Providers.Add < BrotliCompressionProvider > ();options.Providers.Add < GzipCompressionProvider > ();});builder.Services。Configure(options => { options.Level = CompressionLevel.Fastest; }); builder.Services.Configure(options => { options.Level = CompressionLevel.Optimal; }); var app = builder.Build(); // Configure the HTTP request pipeline. app.UseResponseCompression(); app.MapGet("/", () => "Hello World!"); app.Run();

创建一个自定义压缩在ASP提供商。网络核心

您还可以创建自己的压缩和解压缩供应商在ASP。净最小核心api。

要创建一个自定义压缩供应商,首先创建一个类,它实现了ICompressionProvider接口如下所示。

公开课CustomCompressionProvider: ICompressionProvider{公共流CreateStream(流outputStream){/ /编写代码来压缩返回outputStream;}}

接下来注册和配置自定义压缩提供者程序中。cs使用下面的代码。

var builder = WebApplication.CreateBuilder (args);builder.Services。AddResponseCompression(选项= > {options.Providers.Add < BrotliCompressionProvider > ();options.Providers.Add < GzipCompressionProvider > ();options.Providers.Add < CustomCompressionProvider > ();});应用var = builder.Build ();app.UseResponseCompression ();app.MapGet (“/”, () = >“Hello World !”);app.Run ();

在ASP中实现一个控制器。网络核心

既然响应压缩在项目已经安装和配置,让我们实现一个控制器来演示响应压缩。要做到这一点,选择项目的控制器解决方案文件夹,然后单击Add - >控制器。选择“API控制器——空”模板从模板显示在列表中添加脚手架窗口并输入一个名称当提示的控制器。然后用以下代码替换默认的。

[生产(application / json)][路线(“api /违约”)]公共类DefaultController:控制器{/ /得到:api /违约(HttpGet)公共列表<消息>得到(){列表<消息> lst = new <消息> ();for (int指数= 0;指数< 100;指数+ +){消息消息=新消息();消息。文本=。”“这是一个短信;lst.Add(消息);}返回lst;}}

消息类包含一个字符串属性。

公开课信息{公共字符串文本{得到;设置;}}

当我执行这个控制器方法的时候,我可以看到压缩响应的大小是0.091 KB。压缩残疾时,响应的大小是3.419 KB。禁用响应压缩,您可以简单地在程序中注释掉相应的行。cs文件。

/ / builder.Services.AddResponseCompression ();/ / app.UseResponseCompression ();

为响应压缩工作,客户端必须通知服务器的功能通过发送请求的接受编码标头。服务器反过来应该包含这个头压缩响应通知客户端,响应被压缩。

注意,响应压缩在ASP。网络核心is flexible, easy to configure, and easy to use, it is slower than IIS compression. You’ll want to rely on the compression provided by IIS and other web servers whenever you can. You can learn more about response compression in ASP.NET Core from微软的ASP。网络核心documentation

使用HTTPS压缩

请注意,压缩/ HTTPS可以暴露敏感数据犯罪和违反攻击。攻击者可以拦截HTTPS压缩交通偷凭证或会话cookie,危及安全的应用程序。你可以防止这种攻击通过配置web服务器利用TLS而不是HTTPS压缩。

TLS层压缩压缩数据高于HTTPS,使得攻击者更难利用。与传统的压缩技术,在应用程序层压缩内容,TLS压缩压缩数据在传输层。

版权©2023 IDG通信公司。

奖2023年信息世界的技术。现在打开条目!