protected override bool Accept(RoutePath path) { if (path.Current != null) { path.Consume(); return(true); } return(false); }
protected override bool Accept(RoutePath path) { if (path.Current != null && path.Current.Equals(Literal, StringComparison.OrdinalIgnoreCase)) { path.Consume(); return(true); } return(false); }
public void GenerateRoutesForController(RouteTree routeTree, Type controllerType) { var routeAttributes = controllerType.GetCustomAttributes(typeof(RouteAttribute), true); var routeAttribute = routeAttributes.Length > 0 ? (RouteAttribute)routeAttributes[0] : null; string route; if (routeAttribute == null) { route = GenerateDefaultRouteForController(controllerType); } else { route = routeAttribute.Value; } var routePath = new RoutePath(route); var currentNode = (IRouteNode)routeTree; var leafNodes = new List <RouteNode>(); do { RouteNode nextNode = null; if (routePath.Current != null) { var routePart = routePath.Consume(); var part = new RouteLiteral(routePart, true); nextNode = new RouteNode(part); AddNode(currentNode, nextNode); if (routePath.Current == null) { part.RouteData[RouteData.ControllerKey] = controllerType; leafNodes.Add(nextNode); } } // If defined as a default route, then we don't require the last path part if (routePath.Current == null) { var defaultAttributes = controllerType.GetCustomAttributes(typeof(DefaultAttribute), true); if (defaultAttributes.Length > 0) { var defaultNode = new RouteNode(new RouteDefault(RouteData.ControllerKey, controllerType)); AddNode(currentNode, defaultNode); leafNodes.Add(defaultNode); } } currentNode = nextNode; }while (routePath.Current != null); foreach (var method in controllerType.GetMethods().Where(x => x.IsPublic && !x.IsStatic && (typeof(ActionResult).IsAssignableFrom(x.ReturnType) || typeof(Task <ActionResult>).IsAssignableFrom(x.ReturnType)))) { GenerateRoutesForAction(routeTree, controllerType, leafNodes, method); } }
public override void ProcessData(RoutePath path, RouteData data) { base.ProcessData(path, data); var part = path.Consume(); part = HttpUtility.UrlDecode(part); var value = Convert.ChangeType(part, Parameter.ParameterType); data[Parameter.Name] = value; }
protected override bool Accept(RoutePath path) { foreach (var constraint in Constraints) { if (!constraint.Accept(path)) { return(false); } } if (path.Current != null) { path.Consume(); return(true); } return(false); }
protected virtual void ConsumePath(RoutePath path) { path.Consume(); }
private void GenerateRoutesForAction(RouteTree routeTree, Type controllerType, List <RouteNode> parentNodes, MethodInfo actionMethod) { var routeAttributes = actionMethod.GetCustomAttributes(typeof(RouteAttribute), true); var routeAttribute = routeAttributes.Length > 0 ? (RouteAttribute)routeAttributes[0] : null; string route; if (routeAttribute == null) { route = GenerateDefaultRouteForAction(actionMethod); } else { route = routeAttribute.Value; } string httpMethod = null; if (actionMethod.IsDefined(typeof(HttpPostAttribute), false)) { httpMethod = "POST"; } bool addToRoot = false; if (route.StartsWith("/")) { addToRoot = true; } var effectiveNodes = addToRoot ? new[] { (IRouteNode)routeTree } : parentNodes.ToArray(); foreach (var node in effectiveNodes) { var routePath = new RoutePath(route); var currentNode = node; do { RouteNode nextNode = null; if (routePath.Current != null) { var part = routePath.Consume(); if (part == "@") { part = GenerateDefaultRouteForAction(actionMethod); } RoutePart routePart; if (part == "*") { var parameter = actionMethod.GetParameters().Single(); routePart = new RouteWildcard(parameter); } else if (part.StartsWith("{") && part.EndsWith("}")) { var id = part.ChopStart("{").ChopEnd("}"); var parameter = actionMethod.GetParameters().Single(x => x.Name == id); routePart = new RouteVariable(routePath.Current == null, parameter); } else { routePart = new RouteLiteral(part, routePath.Current == null); } nextNode = new RouteNode(routePart); if (routePath.Current == null) { routePart.RouteData[RouteData.ActionKey] = actionMethod; routePart.RouteData[RouteData.ControllerKey] = controllerType; if (httpMethod != null) { routePart.RouteData[RouteData.RequiredHttpMethodKey] = httpMethod; } } nextNode = AddNode(currentNode, nextNode); } if (routePath.Current == null) { if (actionMethod.IsDefined(typeof(DefaultAttribute), false)) { AddNode(currentNode, new RouteNode(new RouteDefault(RouteData.ActionKey, actionMethod))); } } currentNode = nextNode; }while (routePath.Current != null); } }