端点路由(endpoint routing)最早出现在asp.net core2.2,在asp.net core3.0提升为一等公民。
endpoint routing的动机
在端点路由出现之前,我们一般在请求处理管道的末尾,定义mvc中间件解析路由。这种方式意味着在处理管道中,mvc中间件之前的中间件将无法获得路由信息。
路由信息对于某些中间件非常有用,比如cors、认证中间件(认证过程可能会用到路由信息)。
	同时端点路由提炼出端点概念,解耦路由匹配逻辑、请求分发。
endpoint routing中间件
由一对中间件组成:
userouting 将路由匹配添加到中间件管道。该中间件查看应用程序中定义的端点集合,并根据请求选择最佳匹配。useendpoints 将端点执行添加到中间件管道。
mapget、mappost等方法将 处理逻辑连接到路由系统;
其他方法将 asp.net core框架特性连接到路由系统。
- maprazorpages for razor pages
- mapcontrollers for controllers
- maphub< thub> for signalr
- mapgrpcservice< tservice> for grpc
	处于这对中间件上游的 中间件: 始终无法感知 endpoint;
	处于这对中间件之间的 中间件,将会感知到endpoint,并有能力执行附加处理逻辑;
	useendpoint是一个终点中间件;
	没有匹配,则进入useendpoint之后的中间件。
	放置在userouting、useendpoints之间的认证授权中间件可以:
	感知被匹配的端点信息;在调度到endpoint之前,应用授权策略。
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
						 | publicvoidconfigure(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定义了健康检查,该端点定义了iauthorizedatametadata,要求先认证再执行健康检查。
我们在userouting、useendpoints之间添加一点口水代码:感知端点:
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
						 | app.use(next => context =>{var endpoint = context.getendpoint();if(endpoint isnull){returntask.completedtask;}console.writeline($"endpoint: {endpoint.displayname}");if(endpoint isrouteendpoint routeendpoint){console.writeline("endpoint has route pattern: "+routeendpoint.routepattern.rawtext);}foreach(var metadata inendpoint.metadata){console.writeline($"endpoint has metadata: {metadata}");}returnnext(context);}); | 
	当请求/healthz时,感知到authorizeattribute metadata
	故猜想认证授权中间件要对/healthz起作用,必然会对这个 authorizeattribute metadata有所反应。
	于是翻阅githubauthorizationmiddleware3.0源码:发现确实关注了endpoint
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
						 | // ---- 截取自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确实是实现了iauthorizedata接口。
binggo, 猜想得到源码验证。
结论
	端点路由:允许asp.net core应用程序在中间件管道的早期确定要调度的端点,
	以便后续中间件可以使用该信息来提供当前管道配置无法提供的功能。
这使asp.net core框架更加灵活,强化端点概念,它使路由匹配和解析功能与终结点分发功能脱钩。
https://github.com/dotnet/aspnetcore/blob/master/src/security/authorization/policy/src/authorizationmiddleware.cs
到此这篇关于详解asp.net core端点路由的作用原理的文章就介绍到这了,更多相关asp.net core端点路由内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://www.cnblogs.com/JulianHuang/p/13286139.html
相关文章
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
- 
            2025-05-29 102
- 
            2025-05-25 43
- 
            2025-05-27 68
- 
            2025-05-29 44
- 
            2025-06-04 54
 
        


 
    		 
            	 
															 
         
         
        
 
                         
                        