[[419724]]
早先提及了端点路由app.UseEndpoints,让人 端点路由强调的是端点和路由,其核心目的困惑是将请求落地点与路由寻址方式解耦。
《ASP.NET Core端点路由作用原理》
这里面有几个容易混淆的由函函数
有什么不同?什么时候该用哪一个?
Adds endpoints for controller actions to the Microsoft.AspNetCore.Routing.IEndpointRouteBuilder and specifies a route with the given name, pattern, defaults, constraints, and dataTokens.
约定路由(conventional routing), 通常用在MVC项目中;
需要传参name pattern defaults constraints dataTokens;
你可以在项目中这样写:
- endpoints.MapControllerRoute(
- name:"default",
- pattern:"{ controller=Home}/{ action=index}/{ id?}"
- );
如果请求url满足 { host}{ controller_name}{ action_name}{ option_id} , 将命中Controller=controller_name Action=action_name的端点的路方法体;如果url不提供controller、action名称,中种默认命中home/index 方法体。让人
说到底这种写法:
是困惑MVC web项目的早期写法,让用户请求的由函url去匹配开发者的Controller-Action名称。
如今约定路由并不是端点的路主流,因为所谓的中种约定路由对于用户浏览并不友好,而且暴露了后端开发者定义的让人琐碎的Controller、Action名称。困惑
实际上,由函不应该让用户的url去匹配开发者定义的Controller-Action名称(太丑陋的行为),而应该让开发者去匹配用户想要使用的url, 这样特性路由出现了。
Adds endpoints for controller actions to the Microsoft.AspNetCore.Routing.IEndpointRouteBuilder and adds the default route { controller=Home}/{ action=Index}/{ id?}.
endpoints.MapDefaultControllerRoute(); 正是上面约定路由的默认样例,这没什么好聊的。
Adds endpoints for controller actions to the Microsoft.AspNetCore.Routing.IEndpointRouteBuilder without specifying any routes.
不对约定路由做任何假设,也就是不使用约定路由,依赖用户的特性路由, 一般用在WebAPI项目中。
全文梳理就会发现: 官方英文描述屡次出现的route,其实特指的是约定路由。
这样的描述我其实是不苟同的:
路由在.NET里面, 已经被普世认定为“约定路由”和“特性路由”,基于这种认知,我读了好几遍官方英文描述,其实没读出个所以然的。
官方英文描述使用 “route”来特指“约定路由”会误导开发者。
https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/Builder/ControllerEndpointRouteBuilderExtensions.cs
责任编辑:武晓燕 来源: 精益码农 路由函数ASP(责任编辑:综合)
评价结果显示:零售业务对商业银行收益可持续能力的贡献不断增强