/// <summary>
        /// Matches a path, such as "/patients/list", to route, by finding the first route that could
        /// possibly resolve to that path.
        /// </summary>
        /// <param name="requestPath">The request path.</param>
        /// <returns>
        /// A result containing the route, if matched, or a reason for failure if not matched.
        /// </returns>
        public RouteMatch MatchPathToRoute(string requestPath)
        {
            var routes = GetRoutes();

            if (routes.Count == 0)
            {
                return(RouteMatch.Failure(null, "No routes are registered in this route collection."));
            }

            var fails = new List <RouteMatch>();

            foreach (var route in routes)
            {
                var match = route.MatchPathToRoute(requestPath);
                if (match.Success)
                {
                    return(match);
                }
                fails.Add(match);
            }

            return(RouteMatch.Failure(
                       null,
                       string.Join(Environment.NewLine,
                                   fails.Select(fail => string.Format("- Route with specification '{0}' did not match: {1}.", fail.Route, fail.FailReason).CleanErrorMessage()).ToArray()
                                   )));
        }
示例#2
0
        /// <summary>
        /// Attempts to matche a path to this parsed route.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="request">The request.</param>
        /// <returns>An object indicating the success or failure of the attempt to match the path.</returns>
        public RouteMatch MatchPathToRoute(IRoute route, string request)
        {
            var values   = new RouteValueDictionary();
            var iterator = new PathIterator(request);

            foreach (var segment in Segments)
            {
                var match = segment.MatchPath(route, iterator);
                if (match.Success)
                {
                    values.AddRange(match.Values);
                }
                else
                {
                    return(RouteMatch.Failure(route, match.FailReason));
                }
            }

            return(iterator.IsAtEnd
                ? RouteMatch.Successful(route, values)
                : RouteMatch.Failure(route, "Route was initially matched, but the request contains additional unexpected segments"));
        }