public void Merge(HttpActionInformation attribute) { this.HttpMappings = new ReadOnlyDictionary <string, MethodInfo>( new Dictionary <string, MethodInfo>(this.HttpMappings.Concat(attribute.HttpMappings))//combine both sets of mappings ); this.Name = string.IsNullOrEmpty(this.Name) ? attribute.Name : this.Name; this.Order = this.Order > 0 ? this.Order : attribute.Order; }
public static ReadOnlyDictionary <string, HttpActionInformation> GetRouteDictionary(Assembly assembly) { var result = new Dictionary <string, HttpActionInformation>(); foreach (Type type in assembly.GetTypes()) { var apiControllers = type.GetCustomAttributes <ApiControllerAttribute>(true); if (apiControllers.Any()) { var methods = type.GetMethods(); foreach (var method in methods) { var actions = method.GetCustomAttributes <HttpMethodAttribute>(true); if (actions.Any()) { foreach (var action in actions) { var controller = type.Name; var classRoutes = type.GetCustomAttributes <RouteAttribute>(true); if (classRoutes.Any()) { foreach (var route in classRoutes) { var httpMethod = new HttpActionInformation(type, method, action, route); if (result.ContainsKey(httpMethod.Template)) { result[httpMethod.Template].Merge(httpMethod); } else { result.Add(httpMethod.Template, httpMethod); } } } else { var httpMethod = new HttpActionInformation(type, method, action); if (result.ContainsKey(httpMethod.Template)) { result[httpMethod.Template].Merge(httpMethod); } else { result.Add(httpMethod.Template, httpMethod); } } } } } } } return(new ReadOnlyDictionary <string, HttpActionInformation>(result)); }
public static HttpActionInformation GetMatchingRouteOfHighestPriority(HttpRequest requestContext, ReadOnlyDictionary <string, HttpActionInformation> .ValueCollection definedRoutes, out Dictionary <string, object> requestData) { HttpActionInformation result = null; var matchingRoutes = GetMatchingRoutes(requestContext, definedRoutes); if (matchingRoutes.Any()) { var priorityPair = matchingRoutes.OrderBy(r => r.Item1.Order).First(); result = priorityPair.Item1; requestData = priorityPair.Item2; } else { requestData = null; } return(result); }
public override async ValueTask <RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values) { HttpActionInformation matchingRoute = Routing.GetMatchingRouteOfHighestPriority(httpContext.Request, this.HandledRoutes.Values, out Dictionary <string, object> requestData); if (matchingRoute != null) { var routeValues = new RouteValueDictionary(); var httpMethod = httpContext.Request.Method; routeValues.Add("controller", values["controller"]); routeValues.Add("action", matchingRoute.HttpMappings[httpMethod].Name); foreach (var kv in requestData) { routeValues[kv.Key] = kv.Value; //add all fields } values = routeValues; } await Task.CompletedTask; return(values); }