Exemple #1
0
        // Compute the precedence for generating a url
        // e.g.: /api/template          == 5.5
        //       /api/template/{id}     == 5.53
        //       /api/{id:int}          == 5.4
        //       /api/template/{id:int} == 5.54
        public static decimal ComputeOutbound(RouteTemplate template)
        {
            // Each precedence digit corresponds to one decimal place. For example, 3 segments with precedences 2, 1,
            // and 4 results in a combined precedence of 2.14 (decimal).
            var precedence = 0m;

            for (var i = 0; i < template.Segments.Count; i++)
            {
                var segment = template.Segments[i];

                var digit = ComputeOutboundPrecedenceDigit(segment);
                Debug.Assert(digit >= 0 && digit < 10);

                precedence += decimal.Divide(digit, (decimal)Math.Pow(10, i));
            }

            return(precedence);
        }
Exemple #2
0
        // Compute the precedence for matching a provided url
        // e.g.: /api/template == 1.1
        //       /api/template/{id} == 1.13
        //       /api/{id:int} == 1.2
        //       /api/template/{id:int} == 1.12
        public static decimal ComputeMatched(RouteTemplate template)
        {
            // Each precedence digit corresponds to one decimal place. For example, 3 segments with precedences 2, 1,
            // and 4 results in a combined precedence of 2.14 (decimal).
            var precedence = 0m;

            for (var i = 0; i < template.Segments.Count; i++)
            {
                var segment = template.Segments[i];

                var digit = ComputeMatchDigit(segment);
                Debug.Assert(digit >= 0 && digit < 10);

                precedence += decimal.Divide(digit, (decimal)Math.Pow(10, i));
            }

            return precedence;
        }
Exemple #3
0
        public TemplateMatcher(
            RouteTemplate template,
            RouteValueDictionary defaults)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            Template = template;
            Defaults = defaults ?? new RouteValueDictionary();

            // Perf: cache the default value for each parameter (other than complex segments).
            _hasDefaultValue = new bool[Template.Segments.Count];
            _defaultValues   = new object[Template.Segments.Count];

            for (var i = 0; i < Template.Segments.Count; i++)
            {
                var segment = Template.Segments[i];
                if (!segment.IsSimple)
                {
                    continue;
                }

                var part = segment.Parts[0];
                if (!part.IsParameter)
                {
                    continue;
                }

                object value;
                if (Defaults.TryGetValue(part.Name, out value))
                {
                    _hasDefaultValue[i] = true;
                    _defaultValues[i]   = value;
                }
            }

            var routePattern = Template.ToRoutePattern();

            _routePatternMatcher = new RoutePatternMatcher(routePattern, Defaults);
        }
Exemple #4
0
 /// <summary>
 /// Creates a new <see cref="TemplateBinder"/> from the provided <paramref name="template"/> and
 /// <paramref name="defaults"/>.
 /// </summary>
 /// <param name="template">The route template.</param>
 /// <param name="defaults">A collection of extra default values that do not appear in the route template.</param>
 /// <returns>A <see cref="TemplateBinder"/>.</returns>
 public abstract TemplateBinder Create(RouteTemplate template, RouteValueDictionary defaults);
 /// <summary>
 /// Creates a new <see cref="GlobalRouteValuesLinkGeneratorTemplateBinder"/> from the provided <paramref name="template"/> and
 /// <paramref name="defaults"/>.
 /// </summary>
 /// <param name="template">The route template.</param>
 /// <param name="defaults">A collection of extra default values that do not appear in the route template.</param>
 /// <returns>A <see cref="GlobalRouteValuesLinkGeneratorTemplateBinder"/>.</returns>
 public abstract GlobalRouteValuesLinkGeneratorTemplateBinder Create(RouteTemplate template, RouteValueDictionary defaults);