转载

ASP.NET Core的路由[3]:Router的创建者——RouteBuilder

在《 注册URL模式与HttpHandler的映射关系 》演示的实例中,我们总是利用一个RouteBuilder对象来为RouterMiddleware中间件创建所需的Router对象,接下来我们就着重来介绍这个对象。RouteBuilder是我们对所有实现了IRouteBuilder接口的所有类型以及对应对象的统称。[本文已经同步到《ASP.NET Core框架揭秘》之中]

目录

一、RouteBuilder

二、RouteCollection

三、多个Route共享同一个Handler

四、每个Route具有独立的Handler

五、扩展方法MapVerb

一、RouteBuilder

如下面的代码片段所示,RouteBuilder对Router对象的创建体现在它的Build方法上。除此之外,IRouteBuilder接口还定义了一系列属性,我们可以利用它们得到用来注册中间件的ApplicationBuilder和用来提供服务的ServiceProvider。我们可以将多个Router注册到RouteBuilder上,这些注册的Router保存在Routes(不是Routers)属性上,而DefaultHandler属性返回一个默认的Router。

 public interface IRouteBuilder
 {
     IApplicationBuilder      ApplicationBuilder { get; }
     IServiceProvider         ServiceProvider { get; }
     IRouter                  DefaultHandler { get; set; }
     IList<IRouter>           Routes { get; }
  
     IRouter Build();
 }

ASP.NET Core默认使用的是如下一个实现了IRouteBuilder的RouteBuilder类型。如下面的代码片段所示,它的属性ApplicationBuilder是调用构造函数时通过相应的参数指定的,作为服务提供者的ServiceProvider则直接来源于这个ApplicationBuilder对象。至于最为核心的Build方法,我们可以看出它返回的实际上是通过注册的Router对象创建的一个RouteCollection对象。

 public class RouteBuilder : IRouteBuilder
 {
     public IApplicationBuilder    ApplicationBuilder { get; }
     public IServiceProvider       ServiceProvider { get; }
     public IList<IRouter>         Routes { get; }
     public IRouter                DefaultHandler { get; set; }
  
     public RouteBuilder(IApplicationBuilder applicationBuilder) : this(applicationBuilder, null){}
  
     public RouteBuilder(IApplicationBuilder applicationBuilder, IRouter defaultHandler)
     {
         this.ApplicationBuilder     = applicationBuilder;
         this.ServiceProvider        = applicationBuilder.ApplicationServices;
         this.DefaultHandler         = defaultHandler;
         this.Routes                 = new List<IRouter>();
      }
  
     public IRouter Build()
     {
         RouteCollection routes = new RouteCollection();
         foreach (IRouter router in this.Routes)
         {
             routes.Add(router);
         }
         return routes;
     }
 }

二、RouteCollection

一个RouteCollection是一个特殊的Router,因为RouteCollection实现如下了如下这个IRouteCollection接口,后者最终实现了IRouter接口。一个RouteCollection对象实际上是对多个Router对象的封装,我们可以调用其Add方法添加封装的Router对象。

 public interface IRouteCollection : IRouter
 {
     void Add(IRouter router);
 }

为了更能更好的认识RouteCollection,尤其是实现在它的RouteAsync方法中路由解析原理,我们定义了如下这么一个模拟的类型。如下面的代码片段所示,当RouteAsync方法被执行的时候,它会遍历每个注册的Router对象并将当前RouteContext上下文作为参数调用它们的RouteAsync方法,直到遇到第一个与当前请求相匹配的Router。由于只有路由规则与当前请求相匹配的Router才会去设置RouteContext的Handler,所以判断Router是否与当前请求匹配的方法很简单,那就是判断当前RouteContext的Handler属性是否为null。

 public class RouteCollection : IRouteCollection
 {
     private readonly List<IRouter> _routes = new List<IRouter>();
  
     public void Add(IRouter router)
     {
         _routes.Add(router);
     }
  
     public async Task RouteAsync(RouteContext context)
     {
         var snapshot = context.RouteData.PushState(null, values: null, dataTokens: null);
         foreach (var router in _routes)
         {
             context.RouteData.Routers.Add(router);
             try
             {
                 await router.RouteAsync(context);
                 if (null != context.Handler)
                 {
                     break;
                 }
             }
             finally
             {
                 if (null == context.Handler)
                 {
                     snapshot.Restore();
                 }
             }
         }
     }
     public IRouter this[int index]     => _routes[index];
     public int Count                   => _routes.Count;
     …
 }

当整个路由解析流程完成之后,最终的RouteData的状态应该只与那个匹配的Router对象有关。换句话说,对于路由规则与当前请求不匹配的Router来说,针对它们的路由解析过程不应该“污染”最终的这个RouteData对象。为了达到这个目的,上面介绍的关于RouteData的快照机制被应用在这个RouteAsync方法上,上面所示的代码片段也体现了这一点。

由于RouteBuilder对RouterMiddleware中间件提供的Router对象实际上是一个RouteCollection对象,换句话说这其实是一个由多个Router对象组成的“路由表”。所谓的路由注册,本质上就是在这个路由表中添加相应的Router对象。RouteBuilder具有若干扩展方法帮助我们以一种很简洁的方式相这个路由表中添加Router,我们先来介绍如下这四个MapRoute重载。

 public static class MapRouteRouteBuilderExtensions
 {
     public static IRouteBuilder MapRoute(this IRouteBuilder routeBuilder, string name, string template);
     public static IRouteBuilder MapRoute(this IRouteBuilder routeBuilder, string name, string template, object defaults);
     public static IRouteBuilder MapRoute(this IRouteBuilder routeBuilder, string name, string template, object defaults, object constraints);
     public static IRouteBuilder MapRoute(this IRouteBuilder routeBuilder, string name, string template, object defaults, object constraints, object dataTokens);
 }

三、多个Route共享同一个Handler

上述这四个MapRoute方法执行之后在路由表中添加的都是一个Route对象,这个Route对象的名称、路由模板、路由参数的默认值和约束和DataToken都是由对应的参数来指定的。我们知道Route对象其实是对另一个Router对象的封装,那么被封装的究竟是个怎样的Router呢?

ASP.NET Core的路由[3]:Router的创建者——RouteBuilder

如上图所示,被注册的Route对象封装的其实是同一个Router,它就是RouteBuilder的DefaultHandler属性返回的那个Router。换句话说,通过调用这些MapRoute方法注册的Route采用同一个处理器来处理被成功路由的请求。所以当我们采用调用这些方法注册路由的时候要求这个RouteBuilder的DefaultHandler属性作了正确的设置。如下所示的代码体现了最后一个MapRoute方法的实现。

 public static IRouteBuilder MapRoute(this IRouteBuilder routeBuilder, string name, string template, object defaults, object constraints, object dataTokens)
 {   
     IInlineConstraintResolver requiredService = routeBuilder.ServiceProvider.GetRequiredService<IInlineConstraintResolver>();
     routeBuilder.Routes.Add(new Route(routeBuilder.DefaultHandler, name, template, new RouteValueDictionary(defaults), new RouteValueDictionary(constraints), new RouteValueDictionary(dataTokens), requiredService));
     return routeBuilder;
 }

对于我们在《 注册URL模式与HttpHandler的映射关系 》演示的关于获取天气预报信息的实例来说,我们也可以按照如下的形式调用RouteBuilder的MapRoute方法来注册所需的两个路由。为了以“流畅”的链式编程的方式来甚至RouteBuilder的默认处理器,我们特意定义了如下这个扩展方法SetDefaultHandler。

 new WebHostBuilder()
     .ConfigureServices(svcs => svcs.AddRouting())
     .Configure(app =>app.UseRouter(builder=>builder
         .SetDefaultHandler(WeatherForecast)
         .MapRoute("route1", @"weather/{city:regex(^0/d{{2,3}}$)}/{days:int:range(1,4)}")
         .MapRoute("route2", @"weather/{city:regex(^0/d{{2,3}}$)}/{@date}")))        
     …
  
 public static IRouteBuilder SetDefaultHandler(this IRouteBuilder builder, RequestDelegate handler)
 {
     builder.DefaultHandler = new RouteHandler(handler);
     return builder;
 }

对于上面通过调用MapRoute方法注册的两个Route对象来说,我们将路由约束以内联的形式直接定义在路由模板上,其实我们也可以将路由约束作为MapRoute方法的参数。如下面的代码片段所示,我们以不仅以参数的形式设置了路由约束,还设置了路由参数的默认值。

 IRouteConstraint city = new RegexRouteConstraint(@"^0/d{2,3}$");
 IRouteConstraint days = new CompositeRouteConstraint(new IRouteConstraint[] { new IntRouteConstraint(), new RangeRouteConstraint(1, 4) });
  
 new WebHostBuilder()
     .ConfigureServices(svcs => svcs.AddRouting())
     .Configure(app => app.UseRouter(builder => builder
         .SetDefaultHandler(WeatherForecast)
         .MapRoute(
             name            : "route1",
             template        : @"weather/{city}/{days}",
             constraints     : new { city = city, days = days },
             defaults        : new {city="010", days=4 })
         .MapRoute(
             name           : "route2",
             template       : @"weather/{city}/{@date}",
             constraints    : new { city = city},
             defaults        :null)))
     …

四、每个Route具有独立的Handler

上面介绍的这四个MapRoute方法重载都会在路由表中注册一个Route对象,它们都将RouteBuilder的DefaultHandler属性返回的Router作为默认的处理器。如果每个注册的Route具有如下图所示各自不同的请求处理逻辑,我们又该如何注册这样的Route呢?

ASP.NET Core的路由[3]:Router的创建者——RouteBuilder

如果需要为注册的Route指定不同的处理器来处理成功路由的请求,我们可以调用RouteBuilder如下两个同样命名为MapRoute的扩展方法。如上所示的这两个MapRoute方法依然会在路由表中注册一个Route对象。调用第一个方法重载除了需要指定一个路由模板之外,还需要显式指定作为请求处理器的RequestDelegate对象。

 public static class RequestDelegateRouteBuilderExtensions
 { 
     public static IRouteBuilder MapRoute(this IRouteBuilder builder, string template, RequestDelegate handler)
     {
         IInlineConstraintResolver resolver = builder.ServiceProvider.GetService< IInlineConstraintResolver>();
         Route route = new Route(new RouteHandler(handler), template, null, null, null, resolver);
         builder.Routes.Add(route);
         return builder;
     }
  
     public static IRouteBuilder MapRoute(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
     {
         IApplicationBuilder appBuilder = builder.ApplicationBuilder.New();
         action(appBuilder);
         return builder.MapRoute(template, appBuilder.Build());
     }
 }

对于我们实例来说,如果我们使用WeatherForecastForDays方法来返回未来指定天数的天气信息,而使用另一个方法WeatherForecastForDate来返回指定日期的天气信息,那么我们就可以采用如下的形式调用上面这个MapRoute方法来注册所需的两个路由。

 new WebHostBuilder()
     .ConfigureServices(svcs => svcs.AddRouting())
     .Configure(app => app.UseRouter(builder => builder
         .MapRoute(@"/weather/{city:^0/d{{2,3}}$}/{days:int:range(1,2)", WeatherForecastForDays)
         .MapRoute(@"/weather/{city:^0/d{{2,3}}$}/{*date}", WeatherForecastForDate)))
     …
  
 public static Task WeatherForecastForDays(HttpContext context);
 public static Task WeatherForecastForDate(HttpContext context);

另一个MapRoute方法除了接收一个作为路由模板的字符串作为第一个参数之外,它的第二个参数是一个类型为Action<IApplicationBuilder>的委托对象。我们可以利用这个委托注册一个或者多个中间件,这些中间件最终会装换成一个RequestDelegate对象并作为注册Route的处理器。如下所示的代码片段展示了这个方法重载的实现。如果改用这个MapRoute方法来注册我们实例中所需的两个路由,我们可以采用如下的编程方式。

 new WebHostBuilder()
     .ConfigureServices(svcs => svcs.AddRouting())
     .Configure(app => app.UseRouter(builder => builder
         .MapRoute(@"/weather/{city:^0/d{{2,3}}$}/{days:int:range(1,2)",appBuilder => appBuilder.Run(WeatherForecastForDays))
         .MapRoute(@"/weather/{city:^0/d{{2,3}}$}/{*date}", appBuilder=> appBuilder.Run(WeatherForecastForDate))))
 …

五、扩展方法MapVerb

在《 注册URL模式与HttpHandler的映射关系 》演示的实例中,我们实际上是调用RouteBuilder的另一个名为MapGet的扩展方法来进行路由注册的,这个方法要求被成功路由的HTTP请求必须采用GET方法。除了针对GET请求,RouteBuilder还具有如下这些针对POST、PUT和DELETE请求的扩展方法(MapPost、MapPut和MapDelete)。

 public static class RequestDelegateRouteBuilderExtensions
 {   
     public static IRouteBuilder MapGet(this IRouteBuilder builder, string template, RequestDelegate handler);
     public static IRouteBuilder MapGet(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action);
  
     public static IRouteBuilder MapPost(this IRouteBuilder builder, string template, RequestDelegate handler);
     public static IRouteBuilder MapPost(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action);
  
     public static IRouteBuilder MapPut(this IRouteBuilder builder, string template, RequestDelegate handler);
     public static IRouteBuilder MapPut(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action);
  
     public static IRouteBuilder MapDelete(this IRouteBuilder builder, string template, RequestDelegate handler);
     public static IRouteBuilder MapDelete(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action);
  
     public static IRouteBuilder MapVerb(this IRouteBuilder builder, string verb, string template, RequestDelegate handler);
     public static IRouteBuilder MapVerb(this IRouteBuilder builder, string verb, string template, Action<IApplicationBuilder> action);
 }

实际上MapGet、MapPost、MapPut和MapDelete方法重载最终都会调用MapVerb方法,后者可以采用字符串的形式指定任意HTTP方法名称(比如“HEAD”和“OPTIONS”等)。这些方法针对HTTP方法的过滤是同一个类型为HttpMethodRouteConstraint的路由约束来实现的,它要求被路由的请求必须采用指定的方法。这两个MapVerb方法重载的实现原理体现在如下所示的代码片段中。

 public static IRouteBuilder MapVerb(this IRouteBuilder builder, string verb, string template, RequestDelegate handler)
 {
     string[] allowedMethods = new string[] { verb };
     Route item = new Route(new RouteHandler(handler), template, null, new RouteValueDictionary(new { 
     httpMethod = new HttpMethodRouteConstraint(allowedMethods) }), null, GetConstraintResolver(builder));
     builder.Routes.Add(item);
     return builder;
 }
  
 public static IRouteBuilder MapVerb(this IRouteBuilder builder, string verb,string template, Action<IApplicationBuilder> action)
 {
     IApplicationBuilder builder2 = builder.ApplicationBuilder.New();
     action(builder2);
     return builder.MapVerb(verb, template, builder2.Build());
 }
原文  http://www.cnblogs.com/artech/p/asp-net-core-routing-03.html
正文到此结束
Loading...