public DefaultLinkGenerator(
            ParameterPolicyFactory parameterPolicyFactory,
            TemplateBinderFactory binderFactory,
            EndpointDataSource dataSource,
            IOptions <RouteOptions> routeOptions,
            ILogger <DefaultLinkGenerator> logger,
            IServiceProvider serviceProvider)
        {
            _parameterPolicyFactory = parameterPolicyFactory;
            _binderFactory          = binderFactory;
            _logger          = logger;
            _serviceProvider = serviceProvider;

            // We cache TemplateBinder instances per-Endpoint for performance, but we want to wipe out
            // that cache is the endpoints change so that we don't allow unbounded memory growth.
            _cache = new DataSourceDependentCache <ConcurrentDictionary <RouteEndpoint, TemplateBinder> >(dataSource, (_) =>
            {
                // We don't eagerly fill this cache because there's no real reason to. Unlike URL matching, we don't
                // need to build a big data structure up front to be correct.
                return(new ConcurrentDictionary <RouteEndpoint, TemplateBinder>());
            });

            // Cached to avoid per-call allocation of a delegate on lookup.
            _createTemplateBinder = CreateTemplateBinder;

            _globalLinkOptions = new LinkOptions()
            {
                AppendTrailingSlash   = routeOptions.Value.AppendTrailingSlash,
                LowercaseQueryStrings = routeOptions.Value.LowercaseQueryStrings,
                LowercaseUrls         = routeOptions.Value.LowercaseUrls,
            };
        }
Beispiel #2
0
        /// <summary>
        /// Create instance of <see cref="LinkGenerator"/> that wrapped the real generator inner.
        /// </summary>
        /// <param name="serviceProvider">The service provider</param>
        public OrderLinkGenerator(IServiceProvider serviceProvider)
        {
            inner = (LinkGenerator)ActivatorUtilities.CreateInstance(serviceProvider, typeInner);

            var autoFlag = BindingFlags.NonPublic | BindingFlags.Instance;

            _binderFactory        = serviceProvider.GetRequiredService <TemplateBinderFactory>();
            _createTemplateBinder = CreateTemplateBinder;

            typeInner.GetField(nameof(_createTemplateBinder), autoFlag) !
            .SetValue(inner, _createTemplateBinder);

            _requiredKeys = typeof(TemplateBinder).GetField(nameof(_requiredKeys), autoFlag) !;
        }
Beispiel #3
0
        public PathRouteValuesTransform(string pattern, TemplateBinderFactory binderFactory)
        {
            if (pattern is null)
            {
                throw new System.ArgumentNullException(nameof(pattern));
            }

            if (binderFactory is null)
            {
                throw new System.ArgumentNullException(nameof(binderFactory));
            }

            _binder = binderFactory.Create(RoutePatternFactory.Parse(pattern));
        }
        public OrderLinkGenerator(
            ParameterPolicyFactory parameterPolicyFactory,
            TemplateBinderFactory binderFactory,
            EndpointDataSource dataSource,
            IOptions <RouteOptions> routeOptions,
            IServiceProvider serviceProvider)
        {
            if (typeInner.FullName != typeName)
            {
                throw new NotImplementedException();
            }
            var logger   = serviceProvider.GetService(typeof(ILogger <>).MakeGenericType(typeInner));
            var autoFlag = BindingFlags.NonPublic | BindingFlags.Instance;

            var args = new object[]
            {
                parameterPolicyFactory,
                binderFactory,
                dataSource,
                routeOptions,
                logger,
                serviceProvider
            };

            var ctorInfo = typeInner.GetConstructors()[0];
            var newExp   = Expression.New(ctorInfo, args.Select(o => Expression.Constant(o)));
            var ctor     = Expression.Lambda <Func <LinkGenerator> >(newExp).Compile();

            inner = ctor();

            _binderFactory        = binderFactory;
            _createTemplateBinder = CreateTemplateBinder;
            var fieldInfo = typeInner.GetField(nameof(_createTemplateBinder), autoFlag);

            fieldInfo.SetValue(inner, _createTemplateBinder);

            _requiredKeys = typeof(TemplateBinder).GetField(nameof(_requiredKeys), autoFlag);
        }
Beispiel #5
0
        private static bool TryProcessPrefixTemplate(HttpRequest request, RoutePattern routePattern, out string path)
        {
            // TODO: Do you have a better way to process the prefix template?
            Contract.Assert(request != null);
            Contract.Assert(routePattern != null);

            HttpContext           httpContext    = request.HttpContext;
            TemplateBinderFactory factory        = request.HttpContext.RequestServices.GetRequiredService <TemplateBinderFactory>();
            TemplateBinder        templateBinder = factory.Create(routePattern);

            RouteValueDictionary ambientValues = GetAmbientValues(httpContext);

            var templateValuesResult = templateBinder.GetValues(ambientValues, request.RouteValues);

            if (templateValuesResult == null)
            {
                // We're missing one of the required values for this route.
                path = default;
                return(false);
            }

            if (!templateBinder.TryProcessConstraints(httpContext, templateValuesResult.CombinedValues, out var _, out var _))
            {
                path = default;
                return(false);
            }

            path = templateBinder.BindValues(templateValuesResult.AcceptedValues);
            int index = path.IndexOf("?", StringComparison.Ordinal); // remove the query string

            if (index >= 0)
            {
                path = path.Substring(0, index);
            }

            return(true);
        }
Beispiel #6
0
 /// <summary>
 /// Creates a new <see cref="TransformBuilder"/>
 /// </summary>
 public TransformBuilder(TemplateBinderFactory binderFactory, IRandomFactory randomFactory, ILogger <TransformBuilder> logger)
 {
     _binderFactory = binderFactory ?? throw new ArgumentNullException(nameof(binderFactory));
     _randomFactory = randomFactory ?? throw new ArgumentNullException(nameof(randomFactory));
     _logger        = logger ?? throw new ArgumentNullException(nameof(logger));
 }
Beispiel #7
0
 public PathRouteValuesTransform(string pattern, TemplateBinderFactory binderFactory)
 {
     _ = pattern ?? throw new ArgumentNullException(nameof(pattern));
     _binderFactory = binderFactory ?? throw new ArgumentNullException(nameof(binderFactory));
     Template       = TemplateParser.Parse(pattern);
 }
 public PathTransformFactory(TemplateBinderFactory binderFactory)
 {
     _binderFactory = binderFactory ?? throw new ArgumentNullException(nameof(binderFactory));
 }
Beispiel #9
0
 public StubUrlMaps(TemplateBinderFactory factory)
 {
     _factory = factory;
 }
Beispiel #10
0
 public RoutePatternFormatter(TemplateBinderFactory templateBinderFactory)
 {
     _templateBinderFactory = templateBinderFactory;
 }