如何在 ASP.Net Core 中使用条件中间件

开发 前端
ASP.Net Core 是微软开源的跨平台、可扩展、轻量级的模块化框架,可用于构建高性能的web应用程序。中间件组件可以注入到 ASP.Net Core 请求管道中实现对 Request 和 Response 的定制和修改。

[[378514]]

ASP.Net Core 是微软开源的跨平台、可扩展、轻量级的模块化框架,可用于构建高性能的web应用程序。中间件组件可以注入到 ASP.Net Core 请求管道中实现对 Request 和 Response 的定制和修改。

ASP.Net Core 中间件可以用于检查、路由或者修改流转于Pipeline的Request和Response。本文将会讨论如何使用这些中间件来实现一些高级操作。

Use,Run,Map方法

介绍Use、Map,Run方法常用来一起构建 HTTP Pipeline 管道,下面快速浏览一下这些方法和它们的用途。

  • Use

该方法将会执行一个委托,然后将 交接棒 传给Pipeline的下一个中间件,因该方法短暂拥有交接棒,所以该方法可用于 短路操作。

  • Run

该方法会执行委托并返回结果。

  • Map

该方法将有条件地执行委托并返回结果。

注册中间件

中间件是在 Startup.Configure 中进行注册,调用方法就是 Use*系列扩展方法,下面是注册中间件的语法。

  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
  2.     app.UseMyCustomMiddleware<MyCustomMiddleware>(); 

需要注意的是,中间件的执行顺序和你注册的顺序是保持一致的。

Invoke 方法

每个中间件都包含一个 Invoke() 方法, 这个方法参数是 HttpContext 的一个实例,本中间件的业务逻辑会在下一个中间件的执行前后都会被执行,如果你有点懵的话,可以了解一下什么叫:递归调用,如下面代码注释所示:

  1. public async Task Invoke(HttpContext context) 
  2.     // Write code here that will be executed before the 
  3.     // next middleware is called 
  4.         await _next.Invoke(context); // call next middleware 
  5.     // Write code here that will be executed after the 
  6.     //next middleware is called  

分流 Http 管道

Map系扩展方法,比如:Map 和 MapWhen,它们常用于给 pipeline 管道操作进行分流,前者是基于 Request path 进行分流,后者是基于指定的 谓语动词 进行分流。

下面的代码片段展示了如何使用 Map 方法对 Request Pipeline 进行分流。

  1. public class Startup 
  2.     private static void MapRequestA(IApplicationBuilder app) 
  3.     { 
  4.         app.Run(async context => 
  5.         { 
  6.             await context.Response.WriteAsync("This is MapRequestA"); 
  7.         }); 
  8.     } 
  9.     private static void MapRequestB(IApplicationBuilder app) 
  10.     { 
  11.         app.Run(async context => 
  12.         { 
  13.             await context.Response.WriteAsync("This is MapRequestB"); 
  14.         }); 
  15.     } 
  16.     private static void MapRequestC(IApplicationBuilder app) 
  17.     { 
  18.         app.Run(async context => 
  19.         { 
  20.             await context.Response.WriteAsync("This is MapRequestC"); 
  21.         }); 
  22.     } 
  23.     public void Configure(IApplicationBuilder app) 
  24.     { 
  25.         app.Map("/mapRequestPathA", MapRequestA); 
  26.         app.Map("/mapRequestPathB", MapRequestB); 
  27.         app.Map("/mapRequestPathB", MapRequestC); 
  28.  
  29.         app.Run(async context => 
  30.         { 
  31.             await context.Response.WriteAsync("Hello World!"); 
  32.         }); 
  33.     } 
  34.    //Other methods 

MapWhen 方法接受两个参数: 

  • Func<HttpContext, bool> predicate
  • delegate action

你可以在 Startup.Configure 方法中拒绝 text/xml 格式的 request,如下代码所示:

  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
  2.         { 
  3.             if (env.IsDevelopment()) 
  4.             { 
  5.                 app.UseDeveloperExceptionPage(); 
  6.             } 
  7.             app.MapWhen(context => context.Request.ContentType.Equals 
  8.             ("text/xml", StringComparison.InvariantCultureIgnoreCase), 
  9.             (IApplicationBuilder applicationBuilder) => 
  10.             { 
  11.                 applicationBuilder.Run(async context => 
  12.                 { 
  13.                     await Task.FromResult(context.Response.StatusCode = StatusCodes.Status406NotAcceptable); 
  14.                 }); 
  15.             }); 
  16.             app.UseMvc(); 
  17.         } 

使用 UseWhen

UseWhen方法可以用于有条件的执行中间件,下面的代码片段展示了如果当前 Request 请求路径是以 /api 开头的话,执行一个指定的中间件,代码如下:

  1. app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), applicationBuilder => 
  2.     applicationBuilder.UseCustomMiddleware(); 
  3. }); 

请注意,UseWhen 不像 MapWhen,前者会继续执行后面的中间件逻辑,不管当前 UseWhen 的委托函数返回的是 true 还是 false,如果有点懵的话,可以理解一下下面的代码:

  1. app.UseMiddlewareA(); 
  2. app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), applicationBuilder => 
  3.     applicationBuilder.UseMiddlewareB(); 
  4. }); 
  5. app.UseMiddlewareC(); 

如果中间件没有短路,那么中间件A和C肯定会被执行的,而且当请求路径是以 /api 开头时,中间件B也会被调度。

在 ASP.Net Core 请求处理管道中有一个中间件链,所有请求和响应都流经此管道,当新请求到达时,这些中间件要么处理请求,要么将请求传递给管道中的下一个组件,对于更复杂的请求处理,我们可以使用 Map 和 MapWhen 方法来分流管道,并可以使用 UseWhen 有条件的执行中间件。

译文链接:https://www.infoworld.com/article/3429602/how-to-use-conditional-middleware-in-aspnet-core.html 

 

责任编辑:武晓燕 来源: 码农读书
相关推荐

2019-08-12 08:00:00

ASP.NetASP.Net Cor编程语言

2021-03-17 09:45:31

LazyCacheWindows

2021-02-06 21:40:13

SignalR通讯TypeScript

2021-02-02 16:19:08

Serilog日志框架

2021-03-10 09:40:43

LamarASP容器

2021-01-07 07:39:07

工具接口 Swagger

2021-02-03 13:35:25

ASPweb程序

2021-01-28 22:39:35

LoggerMessa开源框架

2021-02-28 20:56:37

NCache缓存框架

2021-03-03 22:37:16

MediatR中介者模式

2021-01-31 22:56:50

FromServiceASP

2023-10-18 07:32:27

中间件技术HTTP请求

2021-02-07 17:29:04

监视文件接口

2021-06-22 16:59:56

微软.NETC# 软件开发

2021-04-12 07:03:10

轻量级模块化框架

2021-01-04 05:44:54

框架日志

2022-08-01 08:00:00

开发工具跟踪侦听器

2017-10-20 08:52:11

内存缓存并发模式Linux

2009-02-05 14:02:46

SmtpMail发送邮件ASP.NET

2021-04-14 07:35:12

Json格式化日期
点赞
收藏

51CTO技术栈公众号