ASP.NET Core 2.2中的Endpoint路由详解
 更新时间:2019年03月29日 08:19:02   作者:weilence  

这篇文章主要介绍了ASP.NET Core 2.2中的Endpoint路由详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Endpoint路由

在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作用

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中实现。
启用Endpoint路由

源代码见 Github 。也可以获取源代码到本地看。
在 MvcApplicationBuilderExtensions.cs 文件72行的 UseMvc 方法中我们可以看到以下代码:

var options = app.ApplicationServices.GetRequiredService<IOptions<MvcOptions>>();

if (options.Value.EnableEndpointRouting)
{

}
else
{

}

if 之中是 Endpoint 路由的逻辑, else 是传统路由的逻辑。
而 MvcOptions 的构造方法如下所示, EnableEndpointRouting 是通过 CompatibilitySwitch 来控制默认值的,这就是 CompatibilityVersion.Version_2_2 启用 Endpoint 路由的原因。

public MvcOptions()
{
// …
_enableEndpointRouting = new CompatibilitySwitch<bool>(nameof(EnableEndpointRouting));
// …
}

Endpoint路由实现原理

在 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 中间件。

// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseEndpointRouting();

app.UseMiddleware<EndpointTestMiddleware>();

app.UseMvc(routes =>
{
routes.MapRoute(
name: “default”,
template: “{controller=Home}/{action=Index}/{id?}”);
});
}
// EndpointTestMiddleware.cs
public class EndpointTestMiddleware
{
private RequestDelegate _next;

public EndpointTestMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext httpContext)
{
var endpoint = httpContext.Features.Get<IEndpointFeature>()?.Endpoint;
if (endpoint == null)
{
await _next(httpContext);
return;
}
var attruibutes = endpoint.Metadata.OfType<LogAttribute>();
foreach (var attribute in attruibutes)
{
Debug.WriteLine(“————————————————————————“);
Debug.WriteLine(attribute.Message);
Debug.WriteLine(“————————————————————————“);
}
await _next(httpContext);
}
}
// LogAttribute.cs
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public sealed class LogAttribute : Attribute
{
public LogAttribute(string message)
{
Message = message;
}

public string Message { get; set; }
}
// HomeController.cs
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的实现原理,并给出了一个使用的示例。
参考链接:
[ devblogs.microsoft.com/aspnet/asp-net-core-2-2-0-preview1-endpoint-routing/ ]
[ www.stevejgordon.co.uk/asp-net-core-first-look-at-global-routing-dispatcher ]
[ rolandguijt.com/endpoint-routing-in-asp-net-core-2-2-explained/ ]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持华域联盟。

您可能感兴趣的文章:asp.net Core3.0区域与路由配置的方法ASP.NET Core MVC学习教程之路由(Routing)[译]ASP.NET Core 2.0 路由引擎详解详解ASP.NET Core MVC 源码学习:Routing 路由.Net Core路由处理的知识点与方法总结

ASP.NET
Core
Endpoint
路由

相关文章
详解ASP.NET配置文件Web.config这篇文章主要介绍了ASP.NET配置文件Web.config,Web.config是asp.net应用程序中一个很重要的配置文件,需要的朋友可以参考下 2015-10-10
Asp.net MVC利用knockoutjs实现登陆并记录用户的内外网IP及所在城这篇文章主要介绍了 Asp.net MVC利用knockoutjs实现登陆并记录用户的内外网IP及所在城市(推荐),需要的朋友可以参考下 2017-02-02
删除DataTable重复列,只删除其中的一列重复行的解决方法删除DataTable重复列,只删除其中的一列重复行,下面的方法就可以,也许有更好的方法,希望大家多多指教
2013-02-02
浅谈Asp.net Mvc之Action如何传多个参数的方法本篇文章主要介绍了Asp.net Mvc之Action如何传多个参数的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
2017-08-08
浅谈ASP.NET中最简单的自定义控件这篇文章主要简单介绍了ASP.NET中最简单的自定义控件,以及核心代码示例,需要的朋友可以参考下 2015-01-01
Asp.Net Core 通过中间件防止图片盗链的实例本篇文章主要介绍了Asp.Net Core 通过中间件防止图片盗链的实例,具有一定的参考价值,有兴趣的可以了解一下。
2016-12-12
axp.net ScriptManager的简单用法scriptManager的结构,参数,属性说明 2008-11-11
asp.ent(C#)中判断空字符串的3种方法以及性能分析asp.ent(C#)中判断空字符串的3种方法以及性能分析 2009-11-11
Mvc动态注册HttpModule详解本文主要介绍了Mvc动态注册HttpModule的方法。具有很好的参考价值,下面跟着小编一起来看下吧 2017-03-03
c# 操作符?? null coalescing operator?? "null coalescing" operator 是c#新提供的一个操作符,这个操作符提供的功能是判断左侧的操作数是否是null,如果是则返回结果是右侧的操作数;非null则返回左侧的操作数。 2009-06-06

最新评论

声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。