欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解ASP.NET Core端點(diǎn)路由的作用原理

 更新時(shí)間:2020年07月13日 11:13:17   作者:有態(tài)度的小碼甲  
這篇文章主要介紹了詳解ASP.NET Core端點(diǎn)路由的作用原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

端點(diǎn)路由(Endpoint Routing)最早出現(xiàn)在ASP.NET Core2.2,在ASP.NET Core3.0提升為一等公民。

Endpoint Routing的動機(jī)

在端點(diǎn)路由出現(xiàn)之前,我們一般在請求處理管道的末尾,定義MVC中間件解析路由。這種方式意味著在處理管道中,MVC中間件之前的中間件將無法獲得路由信息。

路由信息對于某些中間件非常有用,比如CORS、認(rèn)證中間件(認(rèn)證過程可能會用到路由信息)。

同時(shí)端點(diǎn)路由提煉出端點(diǎn)概念,解耦路由匹配邏輯、請求分發(fā)。

Endpoint Routing中間件

由一對中間件組成:

UseRouting 將路由匹配添加到中間件管道。該中間件查看應(yīng)用程序中定義的端點(diǎn)集合,并根據(jù)請求選擇最佳匹配。UseEndpoints 將端點(diǎn)執(zhí)行添加到中間件管道。

MapGet、MapPost等方法將 處理邏輯連接到路由系統(tǒng);

其他方法將 ASP.NET Core框架特性連接到路由系統(tǒng)。

  • MapRazorPages for Razor Pages
  • MapControllers for controllers
  • MapHub< THub> for SignalR
  • MapGrpcService< TService> for gRPC

處于這對中間件上游的 中間件: 始終無法感知 Endpoint;
處于這對中間件之間的 中間件,將會感知到Endpoint,并有能力執(zhí)行附加處理邏輯;
UseEndpoint是一個終點(diǎn)中間件;
沒有匹配,則進(jìn)入U(xiǎn)seEndpoint之后的中間件。


放置在UseRouting、UseEndpoints之間的認(rèn)證授權(quán)中間件可以:
感知被匹配的端點(diǎn)信息;在調(diào)度到Endpoint之前,應(yīng)用授權(quán)策略。

public void Configure(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定義了健康檢查,該端點(diǎn)定義了IAuthorizeDatametadata,要求先認(rèn)證再執(zhí)行健康檢查。

我們在UseRouting、UseEndpoints之間添加一點(diǎn)口水代碼:感知端點(diǎn):

  app.Use(next => context =>
      {
        var endpoint = context.GetEndpoint();
        if (endpoint is null)
        {
          return Task.CompletedTask;
        }
        Console.WriteLine($"Endpoint: {endpoint.DisplayName}");

        if (endpoint is RouteEndpoint routeEndpoint)
        {
          Console.WriteLine("Endpoint has route pattern: " +
            routeEndpoint.RoutePattern.RawText);
        }

        foreach (var metadata in endpoint.Metadata)
        {
          Console.WriteLine($"Endpoint has metadata: {metadata}");
        }
        return next(context);
      });

當(dāng)請求/healthz時(shí),感知到AuthorizeAttribute metadata

故猜想認(rèn)證授權(quán)中間件要對/healthz起作用,必然會對這個 AuthorizeAttribute metadata有所反應(yīng)。

于是翻閱GithubAuthorizationMiddleware3.0源碼:發(fā)現(xiàn)確實(shí)關(guān)注了Endpoint

// ---- 截取自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確實(shí)是實(shí)現(xiàn)了IAuthorizeData接口。

binggo, 猜想得到源碼驗(yàn)證。

結(jié)論

端點(diǎn)路由:允許ASP.NET Core應(yīng)用程序在中間件管道的早期確定要調(diào)度的端點(diǎn),
以便后續(xù)中間件可以使用該信息來提供當(dāng)前管道配置無法提供的功能。

這使ASP.NET Core框架更加靈活,強(qiáng)化端點(diǎn)概念,它使路由匹配和解析功能與終結(jié)點(diǎn)分發(fā)功能脫鉤。

https://github.com/dotnet/aspnetcore/blob/master/src/Security/Authorization/Policy/src/AuthorizationMiddleware.cs

到此這篇關(guān)于詳解ASP.NET Core端點(diǎn)路由的作用原理的文章就介紹到這了,更多相關(guān)ASP.NET Core端點(diǎn)路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論