/// <summary>
        /// Matches a collection of route values to a path. This method is primarily used when producing a
        /// link or navigating programmatically.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns>
        /// A result containing the route, if matched, or a reason for failure if not matched.
        /// </returns>
        public PathMatch MatchRouteToPath(RouteValueDictionary values)
        {
            var routes = GetRoutes();
            var fails  = new List <PathMatch>();

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

            foreach (var route in routes)
            {
                var match = route.MatchRouteToPath(values);
                if (match.Success)
                {
                    return(match);
                }

                fails.Add(match);
            }

            return(PathMatch.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()
                                   )));
        }
Beispiel #2
0
        /// <summary>
        /// Attempts to matche given route values to this parsed route, building up a probable path that
        /// could be used to navigate to this route.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="values">The values.</param>
        /// <returns>
        /// An object indicating the success or failure of the attempt to match the path.
        /// </returns>
        public PathMatch MatchRouteToPath(IRoute route, RouteValueDictionary values)
        {
            var valueBag      = new RouteValueBag(values);
            var segmentValues = new List <object>();

            foreach (var segment in Segments)
            {
                var match = segment.MatchValues(route, valueBag);
                if (match.Success)
                {
                    segmentValues.Add(match.SegmentValue);
                }
                else
                {
                    return(PathMatch.Failure(route, match.FailReason));
                }
            }

            var allValues = new RouteValueDictionary(Defaults);

            allValues.AddRange(values, true);
            allValues.AddRange(values, true);

            var leftOver = valueBag.GetRemaining();

            foreach (var leftOverItem in leftOver)
            {
                var key = leftOverItem.Key;
                if (Defaults.ContainsKey(key))
                {
                    var leftOverValue = leftOverItem.Value;
                    var defaultValue  = Defaults[leftOverItem.Key];

                    if ((leftOverValue == null || defaultValue == null) && (leftOverValue != defaultValue) ||
                        (leftOverValue != null && !leftOverValue.Equals(defaultValue)))
                    {
                        return(PathMatch.Failure(route, string.Format("The route was a close match, but the value of the '{0}' parameter was expected to be '{1}', but '{2}' was provided instead.", key, defaultValue, leftOverValue)));
                    }
                }
            }

            if (leftOver.Count > 0)
            {
                foreach (var defaultItem in Defaults.Where(item => leftOver.ContainsKey(item.Key)))
                {
                    leftOver.Remove(defaultItem.Key);
                }
            }

            return(PathMatch.Successful(route, allValues, leftOver, segmentValues));
        }