目錄 1. 特性路由註冊 2. 路由解析 生成DataTokens 選擇HttpController 選擇Action 特性路由的目的在於更好的提供restful架構的介面,最近好忙(懶),所以更新速度慢. 特性路由註冊 [Route(模板)] :定義特性路由模板 普通變數 a/b/{c} 預設變數 ...
目錄
1. 特性路由註冊
2. 路由解析
- 生成DataTokens
- 選擇HttpController
- 選擇Action
特性路由的目的在於更好的提供restful架構的介面,最近好忙(懶),所以更新速度慢.
特性路由註冊
[Route(模板)] :定義特性路由模板
普通變數
a/b/{c}
預設變數
a/b/{c=d}
變數約束
a/b/{c:int:range(10,20)}
通配符
a/b/{*c:datetime}
[RoutePrefix("api/demo")] :定義路由首碼
路由解析
通過IRoutePrefix/IHttpRouteInfoProvider,我們可以直接註冊路由,映射到具體的Controller和Action.
當調用MapHttpAttributeRoutes方法時,WebAPI會創建1個唯一的RouteCollectionRoute作為IHttpRoute並添加到路由表中.
MapHttpAttributeRoutes方法:
public static void MapHttpAttributeRoutes(HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider)
{
RouteCollectionRoute aggregateRoute = new RouteCollectionRoute();
configuration.Routes.Add("MS_attributerouteWebApi", (IHttpRoute) aggregateRoute);
Action<HttpConfiguration> previousInitializer = configuration.Initializer;
configuration.Initializer = (Action<HttpConfiguration>) (config =>
{
previousInitializer(config);
aggregateRoute.EnsureInitialized((Func<IReadOnlyCollection<IHttpRoute>>) (() =>
{
subRoutes = new SubRouteCollection();
AttributeRoutingMapper.AddRouteEntries(subRoutes, configuration, constraintResolver, directRouteProvider);
return subRoutes;
}));
});
}
RouteCollectionRoute是特性路由的HttpRoute對象,既是一個IHttpRoute對象,又是一個IHttpRoute集合.並且其中核心方法為GetRouteData(IHttpRoute其他介面都返回為null),
internal class RouteCollectionRoute : IHttpRoute, IReadOnlyCollection<IHttpRoute>, IEnumerable<IHttpRoute>, IEnumerable
{
public IHttpRouteData GetRouteData(string virtualPathRoot, HttpRequestMessage request)
{
List<IHttpRouteData> httpRouteDataList = new List<IHttpRouteData>();
//調用內部的SubRoutes對象
foreach (IHttpRoute subRoute in (IEnumerable<IHttpRoute>) this.SubRoutes)
{
IHttpRouteData routeData = subRoute.GetRouteData(virtualPathRoot, request);
httpRouteDataList.Add(routeData);
}
return (IHttpRouteData) new RouteCollectionRoute.RouteCollectionRouteData((IHttpRoute) this, httpRouteDataList.ToArray());
}
}
在該方法中,我們發現RouteCollectionRoute調用了內部所有的SubRoutes對象.
而其內部的SubRoutes類型實際為SubRouteCollection類型
internal class SubRouteCollection : IReadOnlyCollection<IHttpRoute>, IEnumerable<IHttpRoute>, IEnumerable
{
private readonly List<IHttpRoute> _routes = new List<IHttpRoute>();
private readonly List<RouteEntry> _entries = new List<RouteEntry>();
public IReadOnlyCollection<RouteEntry> Entries{get;}
}
而SubRoutes的創建是在MapHttpAttributeRoutes方法定義,實際調用是在HttpServer的Send方法初始化的.
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.EnsureInitialized();
//...
}
首先驗證一下我們的特性路由註冊位置(定義1個擴展方法)
public static class RouteCollectionExt
{
public static IEnumerable<IHttpRoute> GetSubRoutes(this HttpRouteCollection routes)
{
var route = routes["MS_attributerouteWebApi"];
var prop = route.GetType().GetProperty("SubRoutes", BindingFlags.Instance | BindingFlags.NonPublic);
var subRoutes = prop.GetValue(route) as IEnumerable<IHttpRoute>;
return subRoutes;
}
}
生成DataTokens
DataTokens這次發揮了一定的作用,同時也告訴我們該如何使用它. (在第1節中,我覺得DataTokens是個冗餘設計)
先看下DataTokens上有哪些東西.
private static void ShowSubRoutesTokens(HttpRouteCollection routes)
{
foreach (var subRoute in routes.GetSubRoutes())
{
Console.WriteLine(subRoute.RouteTemplate);
foreach (var dataToken in subRoute.DataTokens)
{
Console.WriteLine("{0,-12}{1}", dataToken.Key, dataToken.Value);
}
Console.WriteLine();
}
}
截圖:
對於DataTokens的actions和precedence的屬性,在DirectRouteBuilder的Build方法中實現
其中actions表示該路由模板對應的actiondescription(在特性路由中,會為每個controller創建獨立的一份子路由.)
而precedence表示匹配的優先順序,對於有約束的優先順序高於無優先順序.(約束分為常量,變數,通配符)
在前2節中,我們講瞭如何選擇Action以及Controller.
實際上,如果使用特性路由.選擇的機制又有些變化.
選擇HttpController
public virtual HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
IHttpRouteData routeData = request.GetRouteData();
if (routeData != null)
{
//在GetDirectRouteController內獲取了特性路由對應的Controller,同時要求匹配的所有特性路由對應的Controller為同一個
HttpControllerDescriptor directRouteController = DefaultHttpControllerSelector.GetDirectRouteController(routeData);
return directRouteController;
}
//普通路由方式
string controllerName = this.GetControllerName(request);
//...
}
選擇Action
public HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
{
var matchingActions = this.FindMatchingActions(controllerContext, false);
//...
}
private List<ApiControllerActionSelector.CandidateActionWithParams> FindMatchingActions(HttpControllerContext controllerContext, bool ignoreVerbs = false)
{
//此處做特性路由判斷
IEnumerable<IHttpRouteData> subRoutes = controllerContext.RouteData.GetSubRoutes();
return subRoutes == null ? 普通路由 : 特性路由;
}
備註:
- 如果我們為特性路由指定了Name,則會自動創建一個IHttpRoute綁定到RouteCollection上.(這步是在HttpConfiguration初始化中最後做判斷完成的)
- 文章中的代碼並非完整WebAPI代碼,一般是經過自己精簡後的.
- 本篇內容使用MarkDown語法編輯
首發地址:http://neverc.cnblogs.com/p/5975086.html