예제 #1
0
 public virtual void ProcessData(RoutePath path, RouteData data)
 {
     ConsumePath(path);
     foreach (var item in RouteData)
     {
         data[item.Key] = item.Value;
     }
 }
예제 #2
0
 protected override bool Accept(RoutePath path)
 {
     if (path.Current != null)
     {
         path.ConsumeAll();
         return(true);
     }
     return(false);
 }
예제 #3
0
 protected override bool Accept(RoutePath path)
 {
     if (path.Current != null && path.Current.Equals(Literal, StringComparison.OrdinalIgnoreCase))
     {
         path.Consume();
         return(true);
     }
     return(false);
 }
예제 #4
0
        public bool AcceptPath(RoutePath path, string httpMethod)
        {
            var requiredHttpMethod = RequiredHttpMethod;

            if (requiredHttpMethod != null && httpMethod != (string)requiredHttpMethod)
            {
                return(false);
            }
            return(Accept(path));
        }
예제 #5
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);
            }
        }
예제 #6
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;
        }
예제 #7
0
 public bool StartsWith(RoutePath routePath)
 {
     if (routePath.remaining.Count > remaining.Count)
     {
         return(false);
     }
     if (remaining.Take(routePath.remaining.Count).SequenceEqual(routePath.remaining))
     {
         return(true);
     }
     return(false);
 }
예제 #8
0
        private IRoutePart[] CalculateRoute(string path, string httpMethod)
        {
            var routePath = new RoutePath(path);
            var route     = new RouteBuilder();

            foreach (var node in RootPaths)
            {
                if (CalculateRoute(routePath, httpMethod, route, node))
                {
                    return(route.ToArray());
                }
            }

            return(null);
        }
예제 #9
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);
 }
예제 #10
0
        public RouteData Apply(string path, string method)
        {
            path = path.ChopEnd("/");
            var routePath = new RoutePath(path.ToLower());
            var route     = CalculateRoute(path, method);

            if (route == null)
            {
                throw new InvalidOperationException("Could not apply the URL path '" + path + "'.  No route supports this path.");
            }

            var routeData = new RouteData();

            routePath = new RoutePath(path);

            foreach (var part in route)
            {
                part.ProcessData(routePath, routeData);
            }

            return(routeData);
        }
예제 #11
0
 protected override void ConsumePath(RoutePath path)
 {
 }
예제 #12
0
 public Pinned(RoutePath path)
 {
     this.path = path;
 }
예제 #13
0
 protected abstract bool Accept(RoutePath path);
예제 #14
0
 protected virtual void ConsumePath(RoutePath path)
 {
     path.Consume();
 }
예제 #15
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);
            }
        }
예제 #16
0
/*
 *      protected override bool IsDuplicate(IRoutePart part)
 *      {
 *          return part is RouteDefault;
 *      }
 */

        protected override bool Accept(RoutePath path)
        {
            // We only want to apply default routes if there's nothing left in the path
            return(path.Current == null);
        }
예제 #17
0
 protected override void ConsumePath(RoutePath path)
 {
     // Prevent default consume since we're going to consume it ourselves below
 }