Пример #1
0
        private InnerAttributeRoute BuildRoute(ActionDescriptorsCollection actions)
        {
            var routeInfos = GetRouteInfos(_constraintResolver, actions.Items);

            // We're creating one AttributeRouteGenerationEntry per action. This allows us to match the intended
            // action by expected route values, and then use the TemplateBinder to generate the link.
            var generationEntries = new List<AttributeRouteLinkGenerationEntry>();
            foreach (var routeInfo in routeInfos)
            {
                generationEntries.Add(new AttributeRouteLinkGenerationEntry()
                {
                    Binder = new TemplateBinder(routeInfo.ParsedTemplate, routeInfo.Defaults),
                    Defaults = routeInfo.Defaults,
                    Constraints = routeInfo.Constraints,
                    Order = routeInfo.Order,
                    Precedence = routeInfo.Precedence,
                    RequiredLinkValues = routeInfo.ActionDescriptor.RouteValueDefaults,
                    RouteGroup = routeInfo.RouteGroup,
                    Template = routeInfo.ParsedTemplate,
                    TemplateText = routeInfo.RouteTemplate,
                    Name = routeInfo.Name,
                });
            }

            // We're creating one AttributeRouteMatchingEntry per group, so we need to identify the distinct set of
            // groups. It's guaranteed that all members of the group have the same template and precedence,
            // so we only need to hang on to a single instance of the RouteInfo for each group.
            var distinctRouteInfosByGroup = GroupRouteInfosByGroupId(routeInfos);
            var matchingEntries = new List<AttributeRouteMatchingEntry>();
            foreach (var routeInfo in distinctRouteInfosByGroup)
            {
                matchingEntries.Add(new AttributeRouteMatchingEntry()
                {
                    Order = routeInfo.Order,
                    Precedence = routeInfo.Precedence,
                    Target = _target,
                    RouteName = routeInfo.Name,
                    RouteTemplate = routeInfo.RouteTemplate,
                    TemplateMatcher = new TemplateMatcher(
                        routeInfo.ParsedTemplate,
                        new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
                        {
                            { AttributeRouting.RouteGroupKey, routeInfo.RouteGroup }
                        }),
                    Constraints = routeInfo.Constraints
                });
            }

            return new InnerAttributeRoute(
                _target,
                matchingEntries,
                generationEntries,
                _routeLogger,
                _constraintLogger,
                actions.Version);
        }
        private ApiDescriptionGroupCollection GetCollection(ActionDescriptorsCollection actionDescriptors)
        {
            var context = new ApiDescriptionProviderContext(actionDescriptors.Items);

            foreach (var provider in _apiDescriptionProviders)
            {
                provider.OnProvidersExecuting(context);
            }

            for (var i = _apiDescriptionProviders.Length - 1; i >= 0; i--)
            {
                _apiDescriptionProviders[i].OnProvidersExecuted(context);
            }

            var groups = context.Results
                .GroupBy(d => d.GroupName)
                .Select(g => new ApiDescriptionGroup(g.Key, g.ToArray()))
                .ToArray();

            return new ApiDescriptionGroupCollection(groups, actionDescriptors.Version);
        }
Пример #3
0
        private static IServiceProvider CreateServices(params ActionDescriptor[] actions)
        {
            var collection = new ActionDescriptorsCollection(actions, version: 0);

            var actionDescriptorProvider = new Mock<IActionDescriptorsCollectionProvider>();
            actionDescriptorProvider
                .Setup(a => a.ActionDescriptors)
                .Returns(collection);

            var services = new Mock<IServiceProvider>();
            services
                .Setup(s => s.GetService(typeof(IActionDescriptorsCollectionProvider)))
                .Returns(actionDescriptorProvider.Object);

            var routeOptions = new Mock<IOptions<RouteOptions>>();
            routeOptions
                .SetupGet(o => o.Value)
                .Returns(new RouteOptions());

            services
                .Setup(s => s.GetService(typeof(IInlineConstraintResolver)))
                .Returns(new DefaultInlineConstraintResolver(routeOptions.Object));

            services
                .Setup(s => s.GetService(typeof(ILoggerFactory)))
                .Returns(NullLoggerFactory.Instance);

            return services.Object;
        }