一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - ASP.NET Core 2.2中的Endpoint路由詳解

ASP.NET Core 2.2中的Endpoint路由詳解

2020-06-10 14:17weilence ASP.NET教程

這篇文章主要介紹了ASP.NET Core 2.2中的Endpoint路由詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Endpoint路由

ASP.NET Core 2.2中,新增了一種路由,叫做 Endpoint (終結點)路由。本文將以往的路由系統稱為 傳統路由 。

本文通過源碼的方式介紹傳統路由和 Endpoint 路由部分核心功能和實現方法,具體功能上的差異見 官方文檔 。

在升級到ASP.NET Core 2.2后,會自動啟用 Endpoint 路由。如果要恢復以往的實現邏輯,需要加入以下代碼:

?
1
2
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 方法中我們可以看到以下代碼:

?
1
2
3
4
5
6
7
8
9
10
var options = app.ApplicationServices.GetRequiredService<IOptions<MvcOptions>>();
 
if (options.Value.EnableEndpointRouting)
{
  ...
}
else
{
  ...
}

if 之中是 Endpoint 路由的邏輯, else 是傳統路由的邏輯。

而 MvcOptions 的構造方法如下所示, EnableEndpointRouting 是通過 CompatibilitySwitch 來控制默認值的,這就是 CompatibilityVersion.Version_2_2 啟用 Endpoint 路由的原因。

?
1
2
3
4
5
6
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 中間件。

?
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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的實現原理,并給出了一個使用的示例。

參考鏈接:

[ 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/ ]

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/Weilence/p/10616567.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本国产在线视频 | a级aaaaaaaa毛片| 日本色女 | 亚洲 综合 欧美在线视频 | 草久热| 四虎在线最新地址公告 | 色婷婷影院在线视频免费播放 | 亚洲日韩精品欧美一区二区 | 日韩欧美一区二区不卡 | 掀开奶罩边躁狠狠躁软学生 | 青草青草伊人精品视频 | 欧美成人另类人妖 | 欧美色精品天天在线观看视频 | 四虎永久免费地址ww417 | 国产大片线上免费观看 | 99热在线精品播放 | 日本一级不卡一二三区免费 | 国产v日韩v欧美v精品专区 | 欧美va在线观看 | 91短视频在线观看2019 | 五月香婷婷 | 亚洲剧情在线 | 19+韩国女主播激情vip视频在线 | 成版人快猫永久破解版 | 狠狠五月天中文字幕 | 国产rpg迷雾之风冷狐破解 | 我被黑人彻底征服的全文 | 俄罗斯freeoo性另类 | 和岳m的小说 | 成人免费影院 | 小草高清视频免费直播 | 99午夜高清在线视频在观看 | 暖暖 免费 高清 日本 中文 | 鬼吹灯之天星术免费观看 | 亚欧有色在线观看免费版高清 | 四虎永久在线精品波多野结衣 | 国产激情视频在线 | juy799大岛优香在线观看 | 嫩草在线视频www免费观看 | 性绞姿始动作动态图 | 亚洲色图第四页 |