/// <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)); }
/// <summary> /// Given a set of route values, extracts any necessary values that would be used by this segment. /// </summary> /// <param name="route">The route.</param> /// <param name="values">The values.</param> /// <returns> /// An object that indicates whether the values were successfully matched. /// </returns> public override SegmentValueMatch MatchValues(IRoute route, RouteValueBag values) { var value = values.Take(parameterName); if (value == UrlParameter.NotSpecified) { if (defaultValue != UrlParameter.NotSpecified) { return(SegmentValueMatch.Successful(defaultValue)); } return(SegmentValueMatch.Successful()); } return(SegmentValueMatch.Successful(value)); }
/// <summary> /// Given a set of route values, extracts any necessary values that would be used by this segment. /// </summary> /// <param name="route">The route.</param> /// <param name="values">The values.</param> /// <returns> /// An object that indicates whether the values were successfully matched. /// </returns> public override SegmentValueMatch MatchValues(IRoute route, RouteValueBag values) { var value = values.Take(parameterName); if (value == UrlParameter.NotSpecified) { if (defaultValue != UrlParameter.NotSpecified) { return(SegmentValueMatch.Successful(defaultValue)); } return(SegmentValueMatch.Failure(string.Format("A value for the parameter '{0}' was not provided", parameterName))); } return(SegmentValueMatch.Successful(value)); }
/// <summary> /// Given a set of route values, extracts any necessary values that would be used by this segment. /// </summary> /// <param name="route">The route.</param> /// <param name="values">The values.</param> /// <returns> /// An object that indicates whether the values were successfully matched. /// </returns> public override SegmentValueMatch MatchValues(IRoute route, RouteValueBag values) { return SegmentValueMatch.Successful(literal); }
/// <summary> /// Given a set of route values, extracts any necessary values that would be used by this segment. /// </summary> /// <param name="route">The route.</param> /// <param name="values">The values.</param> /// <returns> /// An object that indicates whether the values were successfully matched. /// </returns> public override SegmentValueMatch MatchValues(IRoute route, RouteValueBag values) { var value = values.Take(parameterName); if (value == UrlParameter.NotSpecified) { if (defaultValue != UrlParameter.NotSpecified) { return SegmentValueMatch.Successful(defaultValue); } return SegmentValueMatch.Successful(); } return SegmentValueMatch.Successful(value); }
/// <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); }
/// <summary> /// Given a set of route values, extracts any necessary values that would be used by this segment. /// </summary> /// <param name="route">The route.</param> /// <param name="values">The values.</param> /// <returns> /// An object that indicates whether the values were successfully matched. /// </returns> public override SegmentValueMatch MatchValues(IRoute route, RouteValueBag values) { return(SegmentValueMatch.Successful(literal)); }
/// <summary> /// Given a set of route values, extracts any necessary values that would be used by this segment. /// </summary> /// <param name="route">The route.</param> /// <param name="values">The values.</param> /// <returns> /// An object that indicates whether the values were successfully matched. /// </returns> public override SegmentValueMatch MatchValues(IRoute route, RouteValueBag values) { var value = values.Take(parameterName); if (value == UrlParameter.NotSpecified) { if (defaultValue != UrlParameter.NotSpecified) { return SegmentValueMatch.Successful(defaultValue); } return SegmentValueMatch.Failure(string.Format("A value for the parameter '{0}' was not provided", parameterName)); } return SegmentValueMatch.Successful(value); }