Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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 pieces = part.ChopStart("{").ChopEnd("}").Split(':');
                            var id = pieces[0];
                            var parameter = actionMethod.GetParameters().Single(x => x.Name == id);
                            var variable = new RouteVariable(routePath.Current == null, parameter);

                            var constraints = pieces.Skip(1).ToArray();
                            foreach (var name in constraints)
                            {
                                var constraint = RouteConstraints.GetConstraint(name);
                                variable.Constraints.Add(constraint);
                            }

                            routePart = variable;
                        }
                        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);
            }
        }