如何在 ASP.Net Core 中使用條件中間件
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ǔ)法。
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- 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)用,如下面代碼注釋所示:
- public async Task Invoke(HttpContext context)
- {
- // Write code here that will be executed before the
- // next middleware is called
- await _next.Invoke(context); // call next middleware
- // Write code here that will be executed after the
- //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)行分流。
- public class Startup
- {
- private static void MapRequestA(IApplicationBuilder app)
- {
- app.Run(async context =>
- {
- await context.Response.WriteAsync("This is MapRequestA");
- });
- }
- private static void MapRequestB(IApplicationBuilder app)
- {
- app.Run(async context =>
- {
- await context.Response.WriteAsync("This is MapRequestB");
- });
- }
- private static void MapRequestC(IApplicationBuilder app)
- {
- app.Run(async context =>
- {
- await context.Response.WriteAsync("This is MapRequestC");
- });
- }
- public void Configure(IApplicationBuilder app)
- {
- app.Map("/mapRequestPathA", MapRequestA);
- app.Map("/mapRequestPathB", MapRequestB);
- app.Map("/mapRequestPathB", MapRequestC);
- app.Run(async context =>
- {
- await context.Response.WriteAsync("Hello World!");
- });
- }
- //Other methods
- }
MapWhen 方法接受兩個(gè)參數(shù):
- Func<HttpContext, bool> predicate
- delegate action
你可以在 Startup.Configure 方法中拒絕 text/xml 格式的 request,如下代碼所示:
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.MapWhen(context => context.Request.ContentType.Equals
- ("text/xml", StringComparison.InvariantCultureIgnoreCase),
- (IApplicationBuilder applicationBuilder) =>
- {
- applicationBuilder.Run(async context =>
- {
- await Task.FromResult(context.Response.StatusCode = StatusCodes.Status406NotAcceptable);
- });
- });
- app.UseMvc();
- }
使用 UseWhen
UseWhen方法可以用于有條件的執(zhí)行中間件,下面的代碼片段展示了如果當(dāng)前 Request 請(qǐng)求路徑是以 /api 開(kāi)頭的話,執(zhí)行一個(gè)指定的中間件,代碼如下:
- app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), applicationBuilder =>
- {
- applicationBuilder.UseCustomMiddleware();
- });
請(qǐng)注意,UseWhen 不像 MapWhen,前者會(huì)繼續(xù)執(zhí)行后面的中間件邏輯,不管當(dāng)前 UseWhen 的委托函數(shù)返回的是 true 還是 false,如果有點(diǎn)懵的話,可以理解一下下面的代碼:
- app.UseMiddlewareA();
- app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), applicationBuilder =>
- {
- applicationBuilder.UseMiddlewareB();
- });
- 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