经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
为什么ASP.NET Core的路由处理器可以使用一个任意类型的Delegate
来源:cnblogs  作者:Artech  时间:2024/3/18 15:02:54  对本文有异议

毫不夸张地说,路由是ASP.NET Core最为核心的部分。路由的本质就是注册一系列终结点(Endpoint),每个终结点可以视为“路由模式”和“请求处理器”的组合,它们分别用来“选择”和“处理”请求。请求处理器通过RequestDelegate来表示,但是当我们在进行路由编程的时候,却可以使用任意类型的Delegate作为处理器器,这一切的背后是如何实现的呢?

一、指定任意类型的委托处理路由请求
二、参数绑定
三、返回值处理

一、指定任意类型的委托处理路由请求

路由终结点总是采用一个RequestDelegate委托作为请求处理器,上面介绍的这一系列终结点注册的方法提供的也都是RequestDelegate委托。实际上IEndpointConventionBuilder接口还定义了如下这些用来注册终结点的扩展方法,它们接受任意类型的委托作为处理器。

  1. public static class EndpointRouteBuilderExtensions
  2. {
  3. public static RouteHandlerBuilder Map(this IEndpointRouteBuilder endpoints, string pattern, Delegate handler);
  4. public static RouteHandlerBuilder Map(this IEndpointRouteBuilder endpoints, RoutePattern pattern, Delegate handler);
  5. public static RouteHandlerBuilder MapMethods(this IEndpointRouteBuilder endpoints, string pattern, IEnumerable<string> httpMethods, Delegate handler);
  6. public static RouteHandlerBuilder MapGet(this IEndpointRouteBuilder endpoints, string pattern, Delegate handler);
  7. public static RouteHandlerBuilder MapPost(this IEndpointRouteBuilder endpoints, string pattern, Delegate handler);
  8. public static RouteHandlerBuilder MapPut(this IEndpointRouteBuilder endpoints, string pattern, Delegate handler);
  9. public static RouteHandlerBuilder MapDelete(this IEndpointRouteBuilder endpoints, string pattern, Delegate handler);
  10. }

由于表示路由终结点的RouteEndpoint对象总是将RequestDelegate委托作为请求处理器,所以上述这些扩展方法提供的Delegate对象最终还得转换成RequestDelegate类型,两者之间的适配或者类型转换是由如下这个RequestDelegateFactory类型的Create方法完成的。这个方法根据提供的Delegate对象创建一个RequestDelegateResult对象,后者不仅封装了转换生成的RequestDelegate委托,终结点的元数据集合也在其中。RequestDelegateFactoryOptions是为处理器转换提供的配置选项。

  1. public static class RequestDelegateFactory
  2. {
  3. public static RequestDelegateResult Create(Delegate handler,RequestDelegateFactoryOptions options = null);
  4. }
  5.  
  6. public sealed class RequestDelegateResult
  7. {
  8. public RequestDelegate RequestDelegate { get; }
  9. public IReadOnlyList<object> EndpointMetadata { get; }
  10.  
  11. public RequestDelegateResult(RequestDelegate requestDelegate, IReadOnlyList<object> metadata);
  12. }
  13.  
  14. public sealed class RequestDelegateFactoryOptions
  15. {
  16. public IServiceProvider ServiceProvider { get; set; }
  17. public IEnumerable<string> RouteParameterNames { get; set; }
  18. public bool ThrowOnBadRequest { get; set; }
  19. public bool DisableInferBodyFromParameters { get; set; }
  20. }

我并不打算详细介绍从Delegate向RequestDelegate转换的具体流程,而是通过几个简单的实例演示一下提供的各种类型的委托是如何执行的,这里主要涉及“参数绑定”和“返回值处理”两方面的处理策略。

二、参数绑定

既然可以将一个任意类型的委托终结点的处理器,意味着路由系统在执行委托的时候能够自行绑定其输入参数。这里采用的参数绑定策略与ASP.NET MVC的“模型绑定”如出一辙。当定义某个用来处理请求的方法时,我们可以在输入参数上标注一些特性显式指定绑定数据的来源,这些特性大都实现了如下这些接口。从接口命名可以看出,它们表示绑定的目标参数的原始数据分别来源于路由参数、查询字符串、请求报头、请求主体以及依赖注入容器提供的服务。

  1. public interface IFromRouteMetadata
  2. {
  3. string Name { get; }
  4. }
  5.  
  6. public interface IFromQueryMetadata
  7. {
  8. string Name { get; }
  9. }
  10.  
  11. public interface IFromHeaderMetadata
  12. {
  13. string Name { get; }
  14. }
  15.  
  16. public interface IFromBodyMetadata
  17. {
  18. bool AllowEmpty { get; }
  19. }
  20.  
  21. public interface IFromServiceMetadata
  22. {
  23. }

如下这些特性实现了上面这几个接口,它们都定义在“Microsoft.AspNetCore.Mvc”命名空间下,因为它们原本是为了ASP.NET MVC下的模型绑定服务的。值得一提的是FromQueryAttribute特性不被支持,不知道是刻意为之还是把这个漏掉了。

  1. [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
  2. public class FromRouteAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromRouteMetadata
  3. {
  4. public BindingSource BindingSource { get; }
  5. public string Name { get; set; }
  6. }
  7.  
  8. [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
  9. public class FromQueryAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromQueryMetadata
  10. {
  11.  
  12. public BindingSource BindingSource { get; }
  13. public string Name { get; set; }
  14. }
  15.  
  16. [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
  17. public class FromHeaderAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromHeaderMetadata
  18. {
  19. public BindingSource BindingSource { get; }
  20. public string Name { get; set; }
  21. }
  22.  
  23. [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
  24. public class FromBodyAttribute : Attribute, IBindingSourceMetadata, IConfigureEmptyBodyBehavior, IFromBodyMetadata
  25. {
  26. public BindingSource BindingSource { get; }
  27. public EmptyBodyBehavior EmptyBodyBehavior { get; set; }
  28. bool IFromBodyMetadata.AllowEmpty { get; }
  29. }
  30.  
  31. [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
  32. public class FromServicesAttribute : Attribute, IBindingSourceMetadata, IFromServiceMetadata
  33. {
  34. public BindingSource BindingSource { get; }
  35. }

如下这个演示程序调用WebApplication对象的MapPost方法注册了一个采用“/{foo}”作为模板的终结点。作为终结点处理器的委托指向静态方法Handle,我们为这个方法定义了五个参数,分别标注了上述五个特性。我们将五个参数组合成一个匿名对象作为返回值。

  1. using Microsoft.AspNetCore.Mvc;
  2. var app = WebApplication.Create();
  3. app.MapPost("/{foo}", Handle);
  4. app.Run();
  5.  
  6. static object Handle(
  7. [FromRoute] string foo,
  8. [FromQuery] int bar,
  9. [FromHeader] string host,
  10. [FromBody] Point point,
  11. [FromServices] IHostEnvironment environment)
  12. => new { Foo = foo, Bar = bar, Host = host, Point = point, Environment = environment.EnvironmentName };
  13.  
  14. public class Point
  15. {
  16. public int X { get; set; }
  17. public int Y { get; set; }
  18. }

程序启动之后,我们针对“http://localhost:5000/abc?bar=123”这个URL发送了一个POST请求,请求的主体内容为一个Point对象序列化成生成的JSON。如下所示的是请求报文和响应报文的内容,可以看出Handle方法的foo和bar参数分别绑定的是路由参数“foo”和查询字符串“bar”的值,参数host绑定的是请求的Host报头,参数point是请求主体内容反序列化的结果,参数environment则是由针对当前请求的IServiceProvider对象提供的服务。

  1. POST http://localhost:5000/abc?bar=123 HTTP/1.1
  2. Content-Type: application/json
  3. Host: localhost:5000
  4. Content-Length: 18
  5.  
  6. {"x":123, "y":456}
  1. HTTP/1.1 200 OK
  2. Content-Type: application/json; charset=utf-8
  3. Date: Sat, 06 Nov 2021 11:55:54 GMT
  4. Server: Kestrel
  5. Content-Length: 100
  6.  
  7. {"foo":"abc","bar":123,"host":"localhost:5000","point":{"x":123,"y":456},"environment":"Production"}

如果请求处理器方法的参数没有显式指定绑定数据的来源,路由系统也能根据参数的类型尽可能地从当前HttpContext上下文中提取相应的内容予以绑定。针对如下这几个类型,对应参数的绑定源是明确的。

  • HttpContext:绑定为当前HttpContext上下文。
  • HttpRequest:绑定为当前HttpContext上下文的Request属性。
  • HttpResponse: 绑定为当前HttpContext上下文的Response属性。
  • ClaimsPrincipal: 绑定为当前HttpContext上下文的User属性。
  • CancellationToken: 绑定为当前HttpContext上下文的RequestAborted属性。

上述的绑定规则体现在如下演示程序的调试断言中。这个演示实例还体现了另一个绑定规则,那就是只要当前请求的IServiceProvider能够提供对应的服务,对应参数(“httpContextAccessor”)上标注的FromSerrvicesAttribute特性不是必要的但是倘若缺少对应的服务注册,请求的主体内容会一般会作为默认的数据来源,所以FromSerrvicesAttribute特性最好还是显式指定为好。对于我们演示的这个例子,如果我们将前面针对AddHttpContextAccessor方法的调用移除,对应参数的绑定自然会失败,但是错误消息并不是我们希望看到的。

  1. using System.Diagnostics;
  2. using System.Security.Claims;
  3.  
  4. var builder = WebApplication.CreateBuilder();
  5. builder.Services.AddHttpContextAccessor();
  6. var app = builder.Build();
  7. app.MapGet("/", Handle);
  8. app.Run();
  9.  
  10. static void Handle(
  11. HttpContext httpContext,
  12. HttpRequest request,
  13. HttpResponse response,
  14. ClaimsPrincipal user,
  15. CancellationToken cancellationToken,
  16. IHttpContextAccessor httpContextAccessor)
  17. {
  18. var currentContext = httpContextAccessor.HttpContext;
  19. Debug.Assert(ReferenceEquals(httpContext, currentContext));
  20. Debug.Assert(ReferenceEquals(request, currentContext.Request));
  21. Debug.Assert(ReferenceEquals(response, currentContext.Response));
  22. Debug.Assert(ReferenceEquals(user, currentContext.User));
  23. Debug.Assert(cancellationToken == currentContext.RequestAborted);
  24. }

对于字符串类型的参数,路由参数查询字符串是两个候选数据源,前者具有更高的优先级。也就是说如果路由参数和查询字符串均提供了某个参数的值,此时会优先选择路由参数提供的值。我个人倒觉得两种绑定源的优先顺序应该倒过来,查询字符串优先级似乎应该更高。对于我们自定义的类型,对应参数默认由请求主体内容反序列生成。由于请求的主体内容只有一份,所以不能出现多个参数都来源请求主体内容的情况,所以下面代码注册的终结点处理器是不合法的。

  1. var app = WebApplication.Create();
  2. app.MapGet("/", (Point p1, Point p2) => { });
  3. app.Run();
  4.  
  5. public class Point
  6. {
  7. public int X { get; set; }
  8. public int Y { get; set; }
  9. }

如果我们在某个类型中定义了一个名为TryParse的静态方法将指定的字符串表达式转换成当前类型的实例,路由系统在对该类型的参数进行绑定的时候会优先从路由参数和查询字符串中提取相应的内容,并通过调用这个方法生成绑定的参数。

  1. var app = WebApplication.Create();
  2. app.MapGet("/", (Point foobar) => foobar);
  3. app.Run();
  4.  
  5. public class Point
  6. {
  7. public int X { get; set; }
  8. public int Y { get; set; }
  9.  
  10. public Point(int x, int y)
  11. {
  12. X = x;
  13. Y = y;
  14. }
  15. public static bool TryParse(string expression, out Point? point)
  16. {
  17. var split = expression.Trim('(', ')').Split(',');
  18. if (split.Length == 2 && int.TryParse(split[0], out var x) && int.TryParse(split[1], out var y))
  19. {
  20. point = new Point(x, y);
  21. return true;
  22. }
  23. point = null;
  24. return false;
  25. }
  26. }

上面的演示程序为自定义的Point类型定义了一个静态的TryParse方法使我们可以将一个以“(x,y)”形式定义的表达式转换成Point对象。注册的终结点处理器委托以该类型为参数,指定的参数名称为“foobar”。我们在发送的请求中以查询字符串的形式提供对应的表达式“(123,456)”,从返回的内容可以看出参数得到了成功绑定。

image

图1  TryParse方法针对参数绑定的影响

如果某种类型的参数具有特殊的绑定方式,我们还可以将具体的绑定实现在一个按照约定定义的BindAsync方法中。按照约定,这个BindAsync应该定义成返回类型为ValueTask<T>的静态方法,它可以拥有一个类型为HttpContext的参数,也可以额外提供一个ParameterInfo类型的参数,这两个参数分别与当前HttpContext上下文和描述参数的ParameterInfo对象绑定。前面演示实例中为Point类型定义了一个TryParse方法可以替换成如下这个 BingAsync方法。

  1. public class Point
  2. {
  3. public int X { get; set; }
  4. public int Y { get; set; }
  5.  
  6. public Point(int x, int y)
  7. {
  8. X = x;
  9. Y = y;
  10. }
  11.  
  12. public static ValueTask<Point?> BindAsync(HttpContext httpContext, ParameterInfo parameter)
  13. {
  14. Point? point = null;
  15. var name = parameter.Name;
  16. var value = httpContext.GetRouteData().Values.TryGetValue(name!, out var v)
  17. ? v
  18. : httpContext.Request.Query[name!].SingleOrDefault();
  19.  
  20. if (value is string expression)
  21. {
  22. var split = expression.Trim('(', ')')?.Split(',');
  23. if (split?.Length == 2 && int.TryParse(split[0], out var x) && int.TryParse(split[1], out var y))
  24. {
  25. point = new Point(x, y);
  26. }
  27. }
  28. return new ValueTask<Point?>(point);
  29. }
  30. }

三、返回值处理

作为终结点处理器的委托对象不仅对输入参数没有要求,它还可以返回任意类型的对象。如果返回类型为VoidTask或者ValueTask,均表示没有返回值。如果返回类型为String、Task<String>或者ValueTask<String>,返回的字符串将直接作为响应的主体内容,响应的媒体类型会被设置为“text/plain”。对于其他类型的返回值(包括Task<T>或者ValueTask<T>),默认情况都会序列化成JSON作为响应的主体内容,响应的媒体类型会被设置为“application/json”,即使返回的是原生类型(比如Int32)也是如此。

  1. var app = WebApplication.Create();
  2. app.MapGet("/foo", () => "123");
  3. app.MapGet("/bar", () => 123);
  4. app.MapGet("/baz", () => new Point { X = 123, Y = 456});
  5. app.Run();
  6.  
  7. public class Point
  8. {
  9. public int X { get; set; }
  10. public int Y { get; set; }
  11. }

上面的演示程序注册了三个终结点,作为处理器的返回值分别为字符串、整数和Point对象。如果我们针对这三个终结点发送对应的GET请求,将得到如下所示的响应。

  1. HTTP/1.1 200 OK
  2. Content-Type: text/plain; charset=utf-8
  3. Date: Sun, 07 Nov 2021 01:13:47 GMT
  4. Server: Kestrel
  5. Content-Length: 3
  6.  
  7. 123
  1. HTTP/1.1 200 OK
  2. Content-Type: application/json; charset=utf-8
  3. Date: Sun, 07 Nov 2021 01:14:11 GMT
  4. Server: Kestrel
  5. Content-Length: 3
  6.  
  7. 123
  1. HTTP/1.1 200 OK
  2. Content-Type: application/json; charset=utf-8
  3. Date: Sun, 07 Nov 2021 01:14:26 GMT
  4. Server: Kestrel
  5. Content-Length: 17
  6.  
  7. {"x":123,"y":456}

如果曾经从事过ASP.NET MVC应用的开发,应该对IActionResult接口感到很熟悉。定义在Controller类型中的Action方法一般返回会IActionResult(或者Task<IActionResult>和ValueTask<IActionResult>)对象。当Action方法执行结束后,MVC框架会直接调用返回的IActionResult对象的ExecuteResultAsync方法完整最终针对响应的处理。相同的设计同样被“移植”到这里,并为此定义了如下这个IResult接口。

  1. public interface IResult
  2. {
  3. Task ExecuteAsync(HttpContext httpContext);
  4. }

如果终结点处理器方法返回一个IResult对象或者返回一个Task<T>或ValueTask<T>(T实现了IResult接口),那么IResult对象ExecuteAsync方法将用来完成后续针对响应的处理工作。IResult接口具有一系列的原生实现类型,不过它们大都被定义成了内部类型。虽然我们不能直接调用构造函数构建它们,但是我们可以通过调用定义在Results类型中的如下这些静态方法来使用它们。

  1. public static class Results
  2. {
  3. public static IResult Accepted(string uri = null, object value = null);
  4. public static IResult AcceptedAtRoute(string routeName = null, object routeValues = null, object value = null);
  5. public static IResult BadRequest(object error = null);
  6. public static IResult Bytes(byte[] contents, string contentType = null, string fileDownloadName = null, bool enableRangeProcessing = false, DateTimeOffset? lastModified = default, EntityTagHeaderValue entityTag = null);
  7. public static IResult Challenge(AuthenticationProperties properties = null, IList<string> authenticationSchemes = null);
  8. public static IResult Conflict(object error = null);
  9. public static IResult Content(string content, MediaTypeHeaderValue contentType);
  10. public static IResult Content(string content, string contentType = null, Encoding contentEncoding = null);
  11. public static IResult Created(string uri, object value);
  12. public static IResult Created(Uri uri, object value);
  13. public static IResult CreatedAtRoute(string routeName = null, object routeValues = null, object value = null);
  14. public static IResult File(byte[] fileContents, string contentType = null, string fileDownloadName = null, bool enableRangeProcessing = false, DateTimeOffset? lastModified = default, EntityTagHeaderValue entityTag = null);
  15. public static IResult File(Stream fileStream, string contentType = null, string fileDownloadName = null, DateTimeOffset? lastModified = default, EntityTagHeaderValue entityTag = null, bool enableRangeProcessing = false);
  16. public static IResult File(string path, string contentType = null, string fileDownloadName = null, DateTimeOffset? lastModified = default, EntityTagHeaderValue entityTag = null, bool enableRangeProcessing = false);
  17. public static IResult Forbid(AuthenticationProperties properties = null, IList<string> authenticationSchemes = null);
  18. public static IResult Json(object data, JsonSerializerOptions options = null, string contentType = null, int? statusCode = default);
  19. public static IResult LocalRedirect(string localUrl, bool permanent = false, bool preserveMethod = false);
  20. public static IResult NoContent();
  21. public static IResult NotFound(object value = null);
  22. public static IResult Ok(object value = null);
  23. public static IResult Problem(string detail = null, string instance = null, int? statusCode = default, string title = null, string type = null);
  24. public static IResult Redirect(string url, bool permanent = false, bool preserveMethod = false);
  25. public static IResult RedirectToRoute(string routeName = null, object routeValues = null, bool permanent = false, bool preserveMethod = false, string fragment = null);
  26. public static IResult SignIn(ClaimsPrincipal principal, AuthenticationProperties properties = null, string authenticationScheme = null);
  27. public static IResult SignOut(AuthenticationProperties properties = null, IList<string> authenticationSchemes = null);
  28. public static IResult StatusCode(int statusCode);
  29. public static IResult Stream(Stream stream, string contentType = null, string fileDownloadName = null, DateTimeOffset? lastModified = default, EntityTagHeaderValue entityTag = null, bool enableRangeProcessing = false);
  30. public static IResult Text(string content, string contentType = null, Encoding contentEncoding = null);
  31. public static IResult Unauthorized();
  32. public static IResult UnprocessableEntity(object error = null);
  33. public static IResult ValidationProblem(IDictionary<string, string[]> errors, string detail = null, string instance = null, int? statusCode = default, string title = null, string type = null);
  34. }

原文链接:https://www.cnblogs.com/artech/p/18075406/delgate_as_route_handler

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号