static HttpControllerDescriptor GetVersionedController(ControllerSelectionContext context, ApiVersion requestedVersion)
        {
            Contract.Requires(context != null);
            Contract.Requires(requestedVersion != null);

            var directRouteCandidates = context.DirectRouteCandidates;
            var controller            = directRouteCandidates[0].ActionDescriptor.ControllerDescriptor;

            if (directRouteCandidates.Length == 1)
            {
                if (!controller.GetDeclaredApiVersions().Contains(requestedVersion))
                {
                    return(null);
                }
            }
            else
            {
                if ((controller = ResolveController(directRouteCandidates, requestedVersion)) == null)
                {
                    return(null);
                }
            }

            return(controller);
        }
        internal override ControllerSelectionResult SelectController(ControllerSelectionContext context)
        {
            Contract.Requires(context != null);
            Contract.Ensures(Contract.Result <ControllerSelectionResult>() != null);

            var request          = context.Request;
            var requestedVersion = context.RequestedApiVersion;
            var result           = new ControllerSelectionResult()
            {
                HasCandidates    = context.HasAttributeBasedRoutes,
                RequestedVersion = requestedVersion,
            };

            if (!result.HasCandidates)
            {
                return(result);
            }

            var versionNeutralController = result.Controller = GetVersionNeutralController(context.DirectRouteCandidates);

            if (requestedVersion == null)
            {
                if (!AssumeDefaultVersionWhenUnspecified)
                {
                    return(result);
                }

                requestedVersion = ApiVersionSelector.SelectVersion(request, context.AllVersions);

                if (requestedVersion == null)
                {
                    return(result);
                }
            }

            var versionedController = GetVersionedController(context, requestedVersion);

            if (versionedController == null)
            {
                return(result);
            }

            if (versionNeutralController != null)
            {
                throw CreateAmbiguousControllerException(new[] { versionNeutralController, versionedController });
            }

            request.ApiVersionProperties().RequestedApiVersion = requestedVersion;
            result.RequestedVersion = requestedVersion;
            result.Controller       = versionedController;

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Selects and returns the controller descriptor to invoke given the provided request.
        /// </summary>
        /// <param name="request">The <see cref="HttpRequestMessage">request</see> to get a controller descriptor for.</param>
        /// <returns>The <see cref="HttpControllerDescriptor">controller descriptor</see> that matches the specified <paramref name="request"/>.</returns>
        public virtual HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            Arg.NotNull(request, nameof(request));
            Contract.Ensures(Contract.Result <HttpControllerDescriptor>() != null);

            EnsureRequestHasValidApiVersion(request);

            var context = new ControllerSelectionContext(request, GetControllerName, controllerInfoCache);

            if (context.RequestedVersion == null)
            {
                if (options.AssumeDefaultVersionWhenUnspecified)
                {
                    context.RequestedVersion = options.ApiVersionSelector.SelectVersion(context.Request, context.AllVersions);
                }
            }

            var conventionRouteSelector = new ConventionRouteControllerSelector(controllerTypeCache);
            var conventionRouteResult   = default(ControllerSelectionResult);
            var exceptionFactory        = new HttpResponseExceptionFactory(request, new Lazy <ApiVersionModel>(() => context.AllVersions));

            if (context.RouteData == null)
            {
                conventionRouteResult = conventionRouteSelector.SelectController(context);

                if (conventionRouteResult.Succeeded)
                {
                    return(request.ApiVersionProperties().SelectedController = conventionRouteResult.Controller);
                }

                throw exceptionFactory.NewNotFoundOrBadRequestException(conventionRouteResult, default);
            }

            var directRouteSelector = new DirectRouteControllerSelector();
            var directRouteResult   = directRouteSelector.SelectController(context);

            if (directRouteResult.Succeeded)
            {
                return(request.ApiVersionProperties().SelectedController = directRouteResult.Controller);
            }

            conventionRouteResult = conventionRouteSelector.SelectController(context);

            if (conventionRouteResult.Succeeded)
            {
                return(request.ApiVersionProperties().SelectedController = conventionRouteResult.Controller);
            }

            throw exceptionFactory.NewNotFoundOrBadRequestException(conventionRouteResult, directRouteResult);
        }
        public override ControllerSelectionResult SelectController(ControllerSelectionContext context)
        {
            Contract.Requires(context != null);
            Contract.Ensures(Contract.Result <ControllerSelectionResult>() != null);

            var request          = context.Request;
            var requestedVersion = context.RequestedVersion;
            var result           = new ControllerSelectionResult()
            {
                HasCandidates    = context.HasAttributeBasedRoutes,
                RequestedVersion = requestedVersion,
            };

            if (!result.HasCandidates)
            {
                return(result);
            }

            var bestMatches = SelectBestCandidates(context.DirectRouteCandidates, requestedVersion);

            switch (bestMatches.Count)
            {
            case 0:
                break;

            case 1:
                result.Controller = bestMatches.Single();
                break;

            default:
                if (TryDisambiguateControllerByAction(request, bestMatches, out var resolvedController))
                {
                    result.Controller = resolvedController;
                    break;
                }

                throw CreateAmbiguousControllerException(bestMatches);
            }

            return(result);
        }
Esempio n. 5
0
 internal abstract ControllerSelectionResult SelectController(ControllerSelectionContext context);
Esempio n. 6
0
 public abstract ControllerSelectionResult SelectController(ControllerSelectionContext context);