Exemplo n.º 1
0
 /// <summary>
 /// Creates a new instance of <see cref="TemplateBinder"/>.
 /// </summary>
 /// <param name="urlEncoder">The <see cref="UrlEncoder"/>.</param>
 /// <param name="pool">The <see cref="ObjectPool{T}"/>.</param>
 /// <param name="template">The <see cref="RouteTemplate"/> to bind values to.</param>
 /// <param name="defaults">The default values for <paramref name="template"/>.</param>
 public TemplateBinder(
     UrlEncoder urlEncoder,
     ObjectPool <UriBuildingContext> pool,
     RouteTemplate template,
     RouteValueDictionary defaults)
     : this(urlEncoder, pool, template?.ToRoutePattern(), defaults)
 {
 }
Exemplo n.º 2
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);
        }