private static HttpControllerDescriptor GetVersionedRouteController(IHttpRouteData routeData, string versionText) { HttpControllerDescriptor httpControllerDescriptor = null; CandidateAction[] candidates = routeData.GetDirectRouteCandidates(); var version = NuGetVersion.Parse(versionText); if (candidates != null) { //闭包 SemanticVersionedAttribute semanticVersionedAttribute = null; var q = candidates .Where ( (x) => { var rr = false; //闭包 semanticVersionedAttribute = x .ActionDescriptor .GetCustomAttributes <SemanticVersionedAttribute>() .FirstOrDefault(); if (semanticVersionedAttribute != null) { //rr = semanticVersionedAttribute // .AllowedVersionRange // .Satisfies(version); rr = true; } return(rr); } ); q = q.OrderByDescending ( (x) => { return (x .ActionDescriptor .GetCustomAttributes <SemanticVersionedAttribute>() .First() .Version); } , new VersionComparer() ); httpControllerDescriptor = q .First() .ActionDescriptor .ControllerDescriptor; } return(httpControllerDescriptor); }
public static Dictionary <string, Dictionary <string, HttpActionDescriptor> > LoadRoutes ( HttpConfiguration httpConfiguration ) { var assembliesResolver = httpConfiguration .Services .GetAssembliesResolver(); var httpControllerTypeResolver = httpConfiguration .Services .GetHttpControllerTypeResolver(); var controllersTypes = httpControllerTypeResolver .GetControllerTypes(assembliesResolver); var httpControllerSelector = httpConfiguration .Services .GetHttpControllerSelector(); var controllerMapping = httpControllerSelector .GetControllerMapping(); Dictionary <string, Dictionary <string, HttpActionDescriptor> > result = null; if (controllerMapping != null) { var actions = controllerMapping .Values .SelectMany ( (x) => { var httpActionSelector = x .Configuration .Services .GetActionSelector(); var actionsByName = httpActionSelector .GetActionMapping(x); return (actionsByName .SelectMany ( (xx) => { return xx; } )); } ); RoutePrefixAttribute routePrefixAttribute = null; RouteAttribute routeAttribute = null; SemanticVersionedAttribute semanticVersionedAttribute = null; result = actions .Where ( (x) => { #region RoutePrefix + Route + Version 筛选 var r = false; routePrefixAttribute = x .ControllerDescriptor .GetCustomAttributes <RoutePrefixAttribute>() .FirstOrDefault() as RoutePrefixAttribute; if (routePrefixAttribute != null) { routeAttribute = x .GetCustomAttributes <RouteAttribute>() .FirstOrDefault() as RouteAttribute; if (routeAttribute != null) { semanticVersionedAttribute = x .GetCustomAttributes <SemanticVersionedAttribute>() .FirstOrDefault() as SemanticVersionedAttribute; if (semanticVersionedAttribute != null) { r = true; } } } return(r); #endregion } ) .ToLookup ( (x) => { var key = string .Format ( "{1}{0}{2}" , "/" , routePrefixAttribute.Prefix , routeAttribute.Template ); return(key); } ) .ToDictionary ( (x) => { return(x.Key); } , (x) => { return (x .ToDictionary ( (xx) => { var attribute = xx .GetCustomAttributes <SemanticVersionedAttribute>() .FirstOrDefault() as SemanticVersionedAttribute; return attribute .Version .ToNormalizedString(); } )); } ); } return(result); }