public IDictionary <string, object> Match(string routeTemplate, string requestPath, IQueryCollection query = null) { // The TemplateParser can only parse the route part (path), and not the query string. // If the template provided by the user also has a query string, we separate that and match it manually. requestPath = requestPath.SliceTill("?"); var regex = new Regex(@"(.*)(\?[^{}]*$)"); var match = regex.Match(routeTemplate); if (match.Success) { var queryString = match.Groups[2].Value; routeTemplate = match.Groups[1].Value; var queryInTemplate = QueryHelpers.ParseQuery(queryString); if (query?.All(arg => queryInTemplate.ContainsKey(arg.Key.TrimStart('?')) && queryInTemplate[arg.Key.TrimStart('?')] == arg.Value) != true) { return(null); } } var template = TemplateParser.Parse(routeTemplate.SliceTill("?")); var matcher = new TemplateMatcher(template, this.GetDefaults(template)); var values = new RouteValueDictionary(); if (matcher.TryMatch(requestPath.StartsWith("/", StringComparison.OrdinalIgnoreCase) ? requestPath : $"/{requestPath}", values)) { return(this.EnsureParameterConstraints(template, values)); } else { return(null); } }
public RouteValueDictionary Match(string routeTemplate, string requestPath, IQueryCollection query) { // The TemplateParser can only parse the route part, and not the query string. // If the template provided by the user also has a query string, we separate that and match it manually. var regex = new Regex(@"(.*)(\?[^{}]*$)"); var match = regex.Match(routeTemplate); if (match.Success) { var queryString = match.Groups[2].Value; routeTemplate = match.Groups[1].Value; var queryInTemplate = QueryHelpers.ParseQuery(queryString); if (!query.All(arg => queryInTemplate.ContainsKey(arg.Key.TrimStart('?')) && queryInTemplate[arg.Key.TrimStart('?')] == arg.Value)) { return(null); } } var template = TemplateParser.Parse(routeTemplate); var matcher = new TemplateMatcher(template, GetDefaults(template)); var values = new RouteValueDictionary(); return(matcher.TryMatch(requestPath, values) ? values : null); }