詳解ASP.NET Core端點(diǎn)路由的作用原理
端點(diǎn)路由(Endpoint Routing)最早出現(xiàn)在ASP.NET Core2.2,在ASP.NET Core3.0提升為一等公民。
Endpoint Routing的動機(jī)
在端點(diǎn)路由出現(xiàn)之前,我們一般在請求處理管道的末尾,定義MVC中間件解析路由。這種方式意味著在處理管道中,MVC中間件之前的中間件將無法獲得路由信息。
路由信息對于某些中間件非常有用,比如CORS、認(rèn)證中間件(認(rèn)證過程可能會用到路由信息)。
同時(shí)端點(diǎn)路由提煉出端點(diǎn)
概念,解耦路由匹配邏輯、請求分發(fā)。
Endpoint Routing中間件
由一對中間件組成:
UseRouting 將路由匹配添加到中間件管道。該中間件查看應(yīng)用程序中定義的端點(diǎn)集合,并根據(jù)請求選擇最佳匹配。UseEndpoints 將端點(diǎn)執(zhí)行添加到中間件管道。
MapGet、MapPost等方法將 處理邏輯連接到路由系統(tǒng);
其他方法將 ASP.NET Core框架特性連接到路由系統(tǒng)。
- MapRazorPages for Razor Pages
- MapControllers for controllers
- MapHub< THub> for SignalR
- MapGrpcService< TService> for gRPC
處于這對中間件上游的 中間件: 始終無法感知 Endpoint;
處于這對中間件之間的 中間件,將會感知到Endpoint,并有能力執(zhí)行附加處理邏輯;
UseEndpoint是一個終點(diǎn)中間件;
沒有匹配,則進(jìn)入U(xiǎn)seEndpoint之后的中間件。
放置在UseRouting
、UseEndpoints
之間的認(rèn)證授權(quán)中間件可以:
感知被匹配的端點(diǎn)信息;在調(diào)度到Endpoint之前,應(yīng)用授權(quán)策略。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // Matches request to an endpoint. app.UseRouting(); // Endpoint aware middleware. // Middleware can use metadata from the matched endpoint. app.UseAuthentication(); app.UseAuthorization(); // Execute the matched endpoint. app.UseEndpoints(endpoints => { // Configure the Health Check endpoint and require an authorized user. endpoints.MapHealthChecks("/healthz").RequireAuthorization(); // Configure another endpoint, no authorization requirements. endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); }); }
以上在/health
定義了健康檢查,該端點(diǎn)定義了IAuthorizeData
metadata,要求先認(rèn)證再執(zhí)行健康檢查。
我們在UseRouting、UseEndpoints之間添加一點(diǎn)口水代碼:感知端點(diǎn):
app.Use(next => context => { var endpoint = context.GetEndpoint(); if (endpoint is null) { return Task.CompletedTask; } Console.WriteLine($"Endpoint: {endpoint.DisplayName}"); if (endpoint is RouteEndpoint routeEndpoint) { Console.WriteLine("Endpoint has route pattern: " + routeEndpoint.RoutePattern.RawText); } foreach (var metadata in endpoint.Metadata) { Console.WriteLine($"Endpoint has metadata: {metadata}"); } return next(context); });
當(dāng)請求/healthz
時(shí),感知到AuthorizeAttribute
metadata
故猜想認(rèn)證授權(quán)中間件要對/healthz
起作用,必然會對這個 AuthorizeAttribute
metadata有所反應(yīng)。
于是翻閱GithubAuthorizationMiddleware
3.0源碼:發(fā)現(xiàn)確實(shí)關(guān)注了Endpoint
// ---- 截取自https://github.com/dotnet/aspnetcore/blob/master/src/Security/Authorization/Policy/src/AuthorizationMiddleware.cs----- if (endpoint != null) { context.Items[AuthorizationMiddlewareInvokedWithEndpointKey] = AuthorizationMiddlewareWithEndpointInvokedValue; } var authorizeData = endpoint?.Metadata.GetOrderedMetadata<IAuthorizeData>() ?? Array.Empty<IAuthorizeData>(); var policy = await AuthorizationPolicy.CombineAsync(_policyProvider, authorizeData); if (policy == null) { await _next(context); return; } var policyEvaluator = context.RequestServices.GetRequiredService<IPolicyEvaluator>(); ......
而AuthorizeAttribute
確實(shí)是實(shí)現(xiàn)了IAuthorizeData
接口。
binggo, 猜想得到源碼驗(yàn)證。
結(jié)論
端點(diǎn)路由:允許ASP.NET Core應(yīng)用程序在中間件管道的早期確定要調(diào)度的端點(diǎn),
以便后續(xù)中間件可以使用該信息來提供當(dāng)前管道配置無法提供的功能。
這使ASP.NET Core框架更加靈活,強(qiáng)化端點(diǎn)概念,它使路由匹配和解析功能與終結(jié)點(diǎn)分發(fā)功能脫鉤。
到此這篇關(guān)于詳解ASP.NET Core端點(diǎn)路由的作用原理的文章就介紹到這了,更多相關(guān)ASP.NET Core端點(diǎn)路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解ASP.NET WEB API 之屬性路由
- ASP.NET?Core?MVC路由(Routing)的用法
- ASP.NET Core中的Razor頁面實(shí)現(xiàn)路由功能
- ASP.NET?Core使用功能開關(guān)控制路由訪問操作(續(xù))
- ASP.NET?Core使用功能開關(guān)控制路由訪問操作
- Asp.net Core 如何設(shè)置黑白名單(路由限制)
- ASP.NET Core中自定義路由約束的實(shí)現(xiàn)
- asp.net core webapi項(xiàng)目配置全局路由的方法示例
- asp.net core 屬性路由和約定路由的實(shí)現(xiàn)
相關(guān)文章
asp.net Repeater之非常好的數(shù)據(jù)分頁
asp.net Repeater之非常好的數(shù)據(jù)分頁實(shí)現(xiàn)代碼。2009-07-07.NET C#創(chuàng)建WebService服務(wù)簡單實(shí)例
這篇文章主要為大家詳細(xì)介紹了.NET C# 創(chuàng)建WebService服務(wù)簡單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05深入Lumisoft.NET組件POP3郵件接收與刪除操作的使用詳解
本篇文章對Lumisoft.NET組件POP3郵件接收與刪除操作的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05asp.net core 屬性路由和約定路由的實(shí)現(xiàn)
ASP.NET Core中的WebAPI路由用于映射客戶端請求的URL到服務(wù)器端的控制器和操作方法,路由分為基于屬性的路由和約定路由兩種方式,本文就來介紹一下,感興趣的可以了解一下2025-01-01Asp.net response對象與request對象使用介紹
這篇文章主要介紹了Asp.net response對象與request對象使用,需要的朋友可以參考下2014-04-04.Net加密神器Eazfuscator.NET?2023.2?最新版使用教程
這篇文章主要介紹了.Net加密神器Eazfuscator.NET 2023.2最新版使用教程,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06