| | | 1 | | // Taken & adapted from https://github.com/markvincze/Stubbery/blob/main/src/Stubbery/RequestMatching/RouteMatcher.cs |
| | | 2 | | |
| | | 3 | | using Microsoft.AspNetCore.Routing; |
| | | 4 | | using Microsoft.AspNetCore.Routing.Template; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Http.Services; |
| | | 7 | | |
| | | 8 | | /// <inheritdoc /> |
| | | 9 | | public class RouteMatcher : IRouteMatcher |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | | 12 | | public RouteValueDictionary? Match(string routeTemplate, string route) |
| | | 13 | | { |
| | 1537 | 14 | | var template = TemplateParser.Parse(routeTemplate); |
| | 1537 | 15 | | var matcher = new TemplateMatcher(template, GetDefaults(template)); |
| | 1537 | 16 | | var values = new RouteValueDictionary(); |
| | | 17 | | |
| | 1537 | 18 | | return matcher.TryMatch(route, values) ? values : null; |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | private static RouteValueDictionary GetDefaults(RouteTemplate parsedTemplate) |
| | | 22 | | { |
| | 1537 | 23 | | var result = new RouteValueDictionary(); |
| | | 24 | | |
| | 3148 | 25 | | foreach (var parameter in parsedTemplate.Parameters) |
| | 37 | 26 | | if (parameter.DefaultValue != null) |
| | 0 | 27 | | result.Add(parameter.Name!, parameter.DefaultValue); |
| | | 28 | | |
| | 1537 | 29 | | return result; |
| | | 30 | | } |
| | | 31 | | } |