示例#1
0
 protected override bool Accept(RoutePath path)
 {
     if (path.Current != null)
     {
         path.Consume();
         return(true);
     }
     return(false);
 }
示例#2
0
 protected override bool Accept(RoutePath path)
 {
     if (path.Current != null && path.Current.Equals(Literal, StringComparison.OrdinalIgnoreCase))
     {
         path.Consume();
         return(true);
     }
     return(false);
 }
示例#3
0
        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);
            }
        }
示例#4
0
        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;
        }
示例#5
0
 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);
 }
示例#6
0
 protected virtual void ConsumePath(RoutePath path)
 {
     path.Consume();
 }
示例#7
0
        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);
            }
        }