示例#1
0
 public RouteMatchingVisitor(Dictionary <string, SillyController> registeredControllers)
 {
     Controllers     = registeredControllers;
     Controller      = null;
     Method          = null;
     Vars            = new List <object>();
     currentSegment  = string.Empty;
     segmentConsumed = false;
     matchFailed     = false;
     currentRoute    = null;
 }
        private SillyRoute AddRoute(SupportedHttpMethods httpMethod, string key, string urlPattern, string controller, string method)
        {
            if (String.IsNullOrEmpty(key))
            {
                key = "default";
            }

            SillyRoute route = new SillyRoute(key, urlPattern, controller, method);

            Routes[httpMethod].Add(route);

            return(route);
        }
示例#3
0
        public bool TryMatch(SillyRoute route, string[] pathSegments)
        {
            if (pathSegments == null ||
                route == null)
            {
                return(false);
            }

            currentRoute = route;

            int index    = 0;
            int consumed = 0;

            for (; consumed < pathSegments.Length; ++consumed)
            {
                if (index >= route.Count)
                {
                    return(false);
                }

                currentSegment  = pathSegments[consumed].ToLower();
                segmentConsumed = false;

                for (; index < route.Count && !segmentConsumed; ++index)
                {
                    SillySegment segment = route[index];

                    segment.Visit(this);

                    if (matchFailed)
                    {
                        return(false);
                    }
                }
            }

            currentSegment = string.Empty;

            if (index < route.Count)
            {
                currentSegment = string.Empty;

                for (; index < route.Count; ++index)
                {
                    SillySegment segment = route[index];

                    segment.Visit(this);

                    if (matchFailed)
                    {
                        return(false);
                    }
                }
            }

            if (Controller == null)
            {
                Controller = GetController(route.Controller);
            }

            if (Method == null &&
                Controller != null)
            {
                AssignMethod(route.Method, string.Empty);
            }

            return(Controller != null && Method != null);
        }