//Initialize the controller method with all the required parameters, caching expensive //things such as the parameters' info or the methodInfo object. internal ControllerMethod(RouteMask route, MethodInfo method, BaseController instance) { MethodInfo = method; ParameterInfo = MethodInfo.GetParameters(); ReturnType = MethodInfo.ReturnType; Route = route; Name = method.Name; ControllerInstance = instance; }
//This takes a request mask and mashes the data together with the template mask. internal static RouteMask GetCompleteMask(RouteMask requestMask, RouteMask templateMask) { for (int i = 0; i < requestMask.Segments.Length; i++) { //Add the missing variable name to the mask requestMask.Segments[i].VariableName = templateMask.Segments[i].VariableName; requestMask.Segments[i].Variable = templateMask.Segments[i].Variable; } return requestMask; }
//Given a mask, we search the routing tables for a controller mask that would match up internal ControllerMethod GetControllerMethodFromMask(RouteMask mask) { if (RoutingTable[mask.Segments.Length] != null) { //Get all matches in the controller methods //where the route is equal to the mask IEnumerable<ControllerMethod> matches = RoutingTable[mask.Segments.Length].Where(r => r.Route == mask); //Sort matches from least variables to most but also grab and compare with the //item before the returning item and warn if both items are seemingly identical. //This lets users know if there's a potentially valid pair of controller functions //that inadvertedly conflict. IOrderedEnumerable<ControllerMethod> sortedMatches = matches.OrderBy(x => x.Route.VariableCount); //Return the first result if we have any matches return sortedMatches.Any() ? sortedMatches.First() : null; } return null; }