BlackLinks.Routing.RouteWalker.Walk C# (CSharp) Method

Walk() public method

public Walk ( ) : RouteEvaluation
return RouteEvaluation
        public RouteEvaluation Walk()
        {
            if(string.IsNullOrEmpty(this.ResourcePath))
                this.ResourcePath = "/";

            this.PathParts = new List<string>();
            StringBuilder sb = new StringBuilder();
            foreach(char c in this.ResourcePath)
            {
                if(c == '/')
                {
                    if(this.PathParts.Count == 0)
                    {
                        this.PathParts.Add("/");
                        sb = null;
                        continue;
                    }
                    else
                    {
                        this.PathParts.Add(sb.ToString());
                        sb = null;
                        continue;
                    }
                }
                else
                {
                    if(sb == null) sb = new StringBuilder();
                    sb.Append(c);
                }
            }
            if(sb != null)
                this.PathParts.Add(sb.ToString());

            this.LastPartIndex = this.PathParts.Count -1;

            while(MoveNextPart())
            {
                var part = CurrentPart;

                if(part.IsSlash()) //the only slash we ever found is the root.
                {
                    this.LastEvaluatedRoute = this.router.RootRoute;
                    if(this.IsLastPart)
                    {
                        return new RouteEvaluation
                        {
                            Route = this.LastEvaluatedRoute
                        };
                    }
                }
                else //is not root, then we either dynamic... shit or sub...shit :)
                {
                    Route memberRoute = null;
                    if(this.LastEvaluatedRoute.MemberRoutes.Count != 0)
                    {
                        memberRoute = (from r in this.LastEvaluatedRoute.MemberRoutes where string.Compare(r.Name,part,true) == 0 select r).FirstOrDefault();
                        if(memberRoute != null)
                        {
                            this.LastEvaluatedRoute = memberRoute;
                            if(this.IsLastPart)
                            {
                                return new RouteEvaluation
                                {
                                    Route = this.LastEvaluatedRoute
                                };
                            }
                        }
                    }
                    if(memberRoute == null && this.LastEvaluatedRoute.DynamicRoute != null)
                    {
                        var action = this.Context.ActivateAction(this.LastEvaluatedRoute.DynamicRoute);
                        if(action.Execute(ActionExecuteType.Filters))
                        {
                            this.LastEvaluatedRoute = this.LastEvaluatedRoute.DynamicRoute;
                        }
                        if(this.IsLastPart)
                        {
                            return new RouteEvaluation
                            {
                                InstantiatedAction = action,
                                Route = this.LastEvaluatedRoute
                            };
                        }
                    }
                }
            }
            return new RouteEvaluation();
        }

Usage Example

Example #1
0
        /// <summary>
        /// Evaluates the Routing tree and return at least one route.
        /// </summary>
        /// <param name="resourcePath">
        /// HTTP path to the requested resource.
        /// </param>
        /// <returns>
        /// Returns a route to execute.
        /// </returns>
        public RouteEvaluation Evaluate(BlackContext context)
        {
            if (this.RootRoute == null)
            {
                throw new BlackException("Router requires at least a root route", null);
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            RouteWalker walker = new RouteWalker();

            walker.router       = this;
            walker.ResourcePath = context.ResourcePath;
            walker.Context      = context;
            var finalRoute = walker.Walk();

            if (finalRoute.Route != null)
            {
                return(finalRoute);
            }
            return(new RouteEvaluation
            {
                Route = NotFoundRoute
            });
        }
All Usage Examples Of BlackLinks.Routing.RouteWalker::Walk