西安建设市场诚信信息平台网站,江苏住房城乡建设厅网站,网站建设智推网,长安网站建设软件在ASP.NET Core 2.2中#xff0c;新增了一种路由#xff0c;叫做Endpoint#xff08;终结点#xff09;路由。本文将以往的路由系统称为传统路由。本文通过源码的方式介绍传统路由和Endpoint路由部分核心功能和实现方法#xff0c;具体功能上的差异见官方文档。在升级到AS… 在ASP.NET Core 2.2中新增了一种路由叫做Endpoint终结点路由。本文将以往的路由系统称为传统路由。本文通过源码的方式介绍传统路由和Endpoint路由部分核心功能和实现方法具体功能上的差异见官方文档。在升级到ASP.NET Core 2.2后会自动启用Endpoint路由。如果要恢复以往的实现逻辑需要加入以下代码services.AddMvc(options options.EnableEndpointRouting false) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);本文分析的源代码基于ASP.NET Core 2.2.3版本的源代码。Endpoint路由与传统路由的区别在于传统路由Url与Action对应关系的处理是在UseMvc中做的。我们无法根据Url获取对应的Action然后进行处理。Endpoint就是将Url与Action的映射关系从Mvc中拆离作为独立使用的中间件。由此带来的好处是我们可以在其他的中间件中使用Controller和Action上的一些信息例如Attruibute。框架也提供了LinkGenerator类来直接根据Endpoint生成链接不再需要HttpContext的信息。另外也提升了一些RPS(Requests per Second)。不过目前Endpoint依然是在UseMvc中调用更多开放的使用方式会在ASP.NET Core 3.0中实现。源代码见Github。也可以获取源代码到本地看。在MvcApplicationBuilderExtensions.cs文件72行的UseMvc方法中我们可以看到以下代码var options app.ApplicationServices.GetRequiredServiceIOptionsMvcOptions();if (options.Value.EnableEndpointRouting){...}else{...}if之中是Endpoint路由的逻辑else是传统路由的逻辑。而MvcOptions的构造方法如下所示EnableEndpointRouting是通过CompatibilitySwitch来控制默认值的这就是CompatibilityVersion.Version_2_2启用Endpoint路由的原因。public MvcOptions(){ _enableEndpointRouting new CompatibilitySwitchbool(nameof(EnableEndpointRouting));}在MvcApplicationBuilderExtensions.cs文件的92-123行的代码是将所有的Controller中的Action转换成Endpoint。在129行的UseEndpointRouting中添加了一个EndpointRoutingMiddleware的中间件这个中间件就是从所有的Endpoint中找到当前路由对应的Endpoint然后放到Feature集合中。在132行的UseEndpoint中添加了一个EndpointMiddleware中间件这个中间件是将EndpointRoutingMiddleware中找到的Endpoint取出根据其中的MetaData信息找到对应的Controller和Action并调用。在UseMvc方法里UseEndpointRouting和UseEndpoint是连续的两个中间件而UseEndpoint是请求的结束这意味着我们自定义的中间件无法取得Endpoint信息。但是通过手动调用UseEndpoint我们还是可以拿到Endpoint路由信息的。下面展示一个使用示例。定义一个LogAttribute类并包含一个Message属性在Action上声明使用。定义一个EndpointTestMiddleware中间件输出LogAttribute的Message属性。手动调用UseEndpointRouting然后调用我们定义的EndpointTestMiddleware中间件。public void Configure(IApplicationBuilder app, IHostingEnvironment env){ app.UseEndpointRouting(); app.UseMiddlewareEndpointTestMiddleware(); app.UseMvc(routes { routes.MapRoute( name: default, template: {controllerHome}/{actionIndex}/{id?}); });}public class EndpointTestMiddleware{private RequestDelegate _next;public EndpointTestMiddleware(RequestDelegate next){ _next next; }public async Task Invoke(HttpContext httpContext){var endpoint httpContext.Features.GetIEndpointFeature()?.Endpoint;if (endpoint null) {await _next(httpContext);return; }var attruibutes endpoint.Metadata.OfTypeLogAttribute();foreach (var attribute in attruibutes) { Debug.WriteLine(------------------------------------------------------------------------); Debug.WriteLine(attribute.Message); Debug.WriteLine(------------------------------------------------------------------------); }await _next(httpContext); }}[AttributeUsage(AttributeTargets.Method, Inherited false, AllowMultiple true)]public sealed class LogAttribute : Attribute{public LogAttribute(string message){ Message message; }public string Message { get; set; }}public class HomeController : Controller{ [Log(Index)]public IActionResult Index(){return View(); } [Log(Privacy)]public IActionResult Privacy(){return View(); }}这样的话我们可以在我们自己的中间件中拿到Endpoint信息然后找到Controller上的LogAttribute然后输出Message。Endpoint是ASP.NET Core 2.2中一种新的路由机制它解决了传统路由难以扩展的问题解决了传统路由与MVC过于耦合的问题并提升了一定的RPS。本文介绍了Endpoint路由简单分析了Endpoint的实现原理并给出了一个使用的示例。参考链接[https://devblogs.microsoft.com/aspnet/asp-net-core-2-2-0-preview1-endpoint-routing/][https://www.stevejgordon.co.uk/asp-net-core-first-look-at-global-routing-dispatcher][https://rolandguijt.com/endpoint-routing-in-asp-net-core-2-2-explained/]原文地址https://www.cnblogs.com/Weilence/p/10616567.html.NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com