自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在 ASP.Net Core 中使用條件中間件

開(kāi)發(fā) 前端
ASP.Net Core 是微軟開(kāi)源的跨平臺(tái)、可擴(kuò)展、輕量級(jí)的模塊化框架,可用于構(gòu)建高性能的web應(yīng)用程序。中間件組件可以注入到 ASP.Net Core 請(qǐng)求管道中實(shí)現(xiàn)對(duì) Request 和 Response 的定制和修改。

[[378514]]

ASP.Net Core 是微軟開(kāi)源的跨平臺(tái)、可擴(kuò)展、輕量級(jí)的模塊化框架,可用于構(gòu)建高性能的web應(yīng)用程序。中間件組件可以注入到 ASP.Net Core 請(qǐng)求管道中實(shí)現(xiàn)對(duì) Request 和 Response 的定制和修改。

ASP.Net Core 中間件可以用于檢查、路由或者修改流轉(zhuǎn)于Pipeline的Request和Response。本文將會(huì)討論如何使用這些中間件來(lái)實(shí)現(xiàn)一些高級(jí)操作。

Use,Run,Map方法

介紹Use、Map,Run方法常用來(lái)一起構(gòu)建 HTTP Pipeline 管道,下面快速瀏覽一下這些方法和它們的用途。

  • Use

該方法將會(huì)執(zhí)行一個(gè)委托,然后將 交接棒 傳給Pipeline的下一個(gè)中間件,因該方法短暫擁有交接棒,所以該方法可用于 短路操作。

  • Run

該方法會(huì)執(zhí)行委托并返回結(jié)果。

  • Map

該方法將有條件地執(zhí)行委托并返回結(jié)果。

注冊(cè)中間件

中間件是在 Startup.Configure 中進(jìn)行注冊(cè),調(diào)用方法就是 Use*系列擴(kuò)展方法,下面是注冊(cè)中間件的語(yǔ)法。

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

需要注意的是,中間件的執(zhí)行順序和你注冊(cè)的順序是保持一致的。

Invoke 方法

每個(gè)中間件都包含一個(gè) Invoke() 方法, 這個(gè)方法參數(shù)是 HttpContext 的一個(gè)實(shí)例,本中間件的業(yè)務(wù)邏輯會(huì)在下一個(gè)中間件的執(zhí)行前后都會(huì)被執(zhí)行,如果你有點(diǎn)懵的話,可以了解一下什么叫:遞歸調(diào)用,如下面代碼注釋所示:

  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系擴(kuò)展方法,比如:Map 和 MapWhen,它們常用于給 pipeline 管道操作進(jìn)行分流,前者是基于 Request path 進(jìn)行分流,后者是基于指定的 謂語(yǔ)動(dòng)詞 進(jìn)行分流。

下面的代碼片段展示了如何使用 Map 方法對(duì) Request Pipeline 進(jìn)行分流。

  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 方法接受兩個(gè)參數(shù): 

  • 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方法可以用于有條件的執(zhí)行中間件,下面的代碼片段展示了如果當(dāng)前 Request 請(qǐng)求路徑是以 /api 開(kāi)頭的話,執(zhí)行一個(gè)指定的中間件,代碼如下:

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

請(qǐng)注意,UseWhen 不像 MapWhen,前者會(huì)繼續(xù)執(zhí)行后面的中間件邏輯,不管當(dāng)前 UseWhen 的委托函數(shù)返回的是 true 還是 false,如果有點(diǎn)懵的話,可以理解一下下面的代碼:

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

如果中間件沒(méi)有短路,那么中間件A和C肯定會(huì)被執(zhí)行的,而且當(dāng)請(qǐng)求路徑是以 /api 開(kāi)頭時(shí),中間件B也會(huì)被調(diào)度。

在 ASP.Net Core 請(qǐng)求處理管道中有一個(gè)中間件鏈,所有請(qǐng)求和響應(yīng)都流經(jīng)此管道,當(dāng)新請(qǐng)求到達(dá)時(shí),這些中間件要么處理請(qǐng)求,要么將請(qǐng)求傳遞給管道中的下一個(gè)組件,對(duì)于更復(fù)雜的請(qǐng)求處理,我們可以使用 Map 和 MapWhen 方法來(lái)分流管道,并可以使用 UseWhen 有條件的執(zhí)行中間件。

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

 

責(zé)任編輯:武曉燕 來(lái)源: 碼農(nóng)讀書(shū)
相關(guān)推薦

2019-08-12 08:00:00

ASP.NetASP.Net Cor編程語(yǔ)言

2021-01-31 22:56:50

FromServiceASP

2021-02-03 13:35:25

ASPweb程序

2021-03-03 22:37:16

MediatR中介者模式

2021-03-10 09:40:43

LamarASP容器

2021-02-28 20:56:37

NCache緩存框架

2021-01-28 22:39:35

LoggerMessa開(kāi)源框架

2021-01-07 07:39:07

工具接口 Swagger

2021-03-17 09:45:31

LazyCacheWindows

2021-02-02 16:19:08

Serilog日志框架

2021-02-06 21:40:13

SignalR通訊TypeScript

2023-10-18 07:32:27

中間件技術(shù)HTTP請(qǐng)求

2021-02-07 17:29:04

監(jiān)視文件接口

2024-07-12 08:19:53

2021-04-12 07:03:10

輕量級(jí)模塊化框架

2021-01-04 05:44:54

框架日志

2021-06-22 16:59:56

微軟.NETC# 軟件開(kāi)發(fā)

2017-10-20 08:52:11

內(nèi)存緩存并發(fā)模式Linux

2022-08-01 08:00:00

開(kāi)發(fā)工具跟蹤偵聽(tīng)器

2025-03-26 07:53:24

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)