Esempio n. 1
0
        private IReadOnlyList <ActionDescriptor> EvaluateActionConstraints(
            RouteContext context,
            IReadOnlyList <ActionDescriptor> actions)
        {
            var candidates = new List <ActionSelectorCandidate>();

            // Perf: Avoid allocations
            for (var i = 0; i < actions.Count; i++)
            {
                var action      = actions[i];
                var constraints = _actionConstraintCache.GetActionConstraints(context.HttpContext, action);
                candidates.Add(new ActionSelectorCandidate(action, constraints));
            }

            var matches = EvaluateActionConstraintsCore(context, candidates, startingOrder: null);

            List <ActionDescriptor> results = null;

            if (matches != null)
            {
                results = new List <ActionDescriptor>(matches.Count);
                // Perf: Avoid allocations
                for (var i = 0; i < matches.Count; i++)
                {
                    var candidate = matches[i];
                    results.Add(candidate.Action);
                }
            }

            return(results);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public ActionDescriptor Select(RouteContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var tree = _decisionTreeProvider.DecisionTree;
            var matchingRouteConstraints = tree.Select(context.RouteData.Values);

            var candidates = new List <ActionSelectorCandidate>();

            // Perf: Avoid allocations
            for (var i = 0; i < matchingRouteConstraints.Count; i++)
            {
                var action      = matchingRouteConstraints[i];
                var constraints = _actionConstraintCache.GetActionConstraints(context.HttpContext, action);
                candidates.Add(new ActionSelectorCandidate(action, constraints));
            }

            var matchingActionConstraints =
                EvaluateActionConstraints(context, candidates, startingOrder: null);

            List <ActionDescriptor> matchingActions = null;

            if (matchingActionConstraints != null)
            {
                matchingActions = new List <ActionDescriptor>(matchingActionConstraints.Count);
                // Perf: Avoid allocations
                for (var i = 0; i < matchingActionConstraints.Count; i++)
                {
                    var candidate = matchingActionConstraints[i];
                    matchingActions.Add(candidate.Action);
                }
            }

            var finalMatches = SelectBestActions(matchingActions);

            if (finalMatches == null || finalMatches.Count == 0)
            {
                return(null);
            }
            else if (finalMatches.Count == 1)
            {
                var selectedAction = finalMatches[0];

                return(selectedAction);
            }
            else
            {
                var actionNames = string.Join(
                    Environment.NewLine,
                    finalMatches.Select(a => a.DisplayName));

                _logger.AmbiguousActions(actionNames);

                var message = Resources.FormatDefaultActionSelector_AmbiguousActions(
                    Environment.NewLine,
                    actionNames);

                throw new AmbiguousActionException(message);
            }
        }