Beispiel #1
0
        static HttpControllerDescriptor GetVersionedController(ApiVersionControllerAggregator aggregator, ApiVersion requestedVersion)
        {
            Contract.Requires(aggregator != null);
            Contract.Requires(requestedVersion != null);

            var directRouteCandidates = aggregator.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);
                }
            }

            if (!controller.HasApiVersionInfo())
            {
                controller.SetApiVersionModel(aggregator.AllVersions);
            }

            return(controller);
        }
        private static HttpControllerDescriptor GetVersionedController(ApiVersionControllerAggregator aggregator, ApiVersion requestedVersion, Lazy <Exception> ambiguousException)
        {
            Contract.Requires(aggregator != null);
            Contract.Requires(requestedVersion != null);
            Contract.Requires(ambiguousException != null);

            var candidates = aggregator.ConventionRouteCandidates;
            var controller = candidates[0];

            if (candidates.Count == 1)
            {
                if (!controller.GetDeclaredApiVersions().Contains(requestedVersion))
                {
                    return(null);
                }
            }
            else
            {
                if ((controller = ResolveController(candidates, requestedVersion, ambiguousException)) == null)
                {
                    return(null);
                }
            }

            if (!controller.HasApiVersionInfo())
            {
                controller.SetApiVersionModel(aggregator.AllVersions);
            }

            controller.SetRelatedCandidates(candidates);
            return(controller);
        }
        internal override ControllerSelectionResult SelectController(ApiVersionControllerAggregator aggregator)
        {
            Contract.Requires(aggregator != null);
            Contract.Ensures(Contract.Result <ControllerSelectionResult>() != null);

            var request          = aggregator.Request;
            var requestedVersion = aggregator.RequestedApiVersion;
            var controllerName   = aggregator.ControllerName;
            var result           = new ControllerSelectionResult()
            {
                RequestedVersion = requestedVersion,
                ControllerName   = controllerName,
                HasCandidates    = aggregator.HasConventionBasedRoutes
            };

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

            var ambiguousException       = new Lazy <Exception>(() => CreateAmbiguousControllerException(aggregator.RouteData.Route, controllerName, controllerTypeCache.GetControllerTypes(controllerName)));
            var versionNeutralController = result.Controller = GetVersionNeutralController(aggregator.ConventionRouteCandidates, ambiguousException);

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

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

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

            var versionedController = GetVersionedController(aggregator, requestedVersion, ambiguousException);

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

            if (versionNeutralController != null)
            {
                throw ambiguousException.Value;
            }

            request.ApiVersionProperties().ApiVersion = requestedVersion;
            result.Controller = versionedController;

            return(result);
        }
Beispiel #4
0
        internal override ControllerSelectionResult SelectController(ApiVersionControllerAggregator aggregator)
        {
            Contract.Requires(aggregator != null);
            Contract.Ensures(Contract.Result <ControllerSelectionResult>() != null);

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

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

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

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

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

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

            var versionedController = GetVersionedController(aggregator, 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);
        }
Beispiel #5
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 aggregator = new ApiVersionControllerAggregator(request, GetControllerName, controllerInfoCache);
            var conventionRouteSelector = new ConventionRouteControllerSelector(options, controllerTypeCache);
            var conventionRouteResult   = default(ControllerSelectionResult);
            var exceptionFactory        = new HttpResponseExceptionFactory(request);

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

                if (conventionRouteResult.Succeeded)
                {
                    return(conventionRouteResult.Controller);
                }

                throw exceptionFactory.NewNotFoundOrBadRequestException(conventionRouteResult, null);
            }

            var directRouteSelector = new DirectRouteControllerSelector(options);
            var directRouteResult   = directRouteSelector.SelectController(aggregator);

            if (directRouteResult.Succeeded)
            {
                return(directRouteResult.Controller);
            }

            conventionRouteResult = conventionRouteSelector.SelectController(aggregator);

            if (conventionRouteResult.Succeeded)
            {
                return(conventionRouteResult.Controller);
            }

            throw exceptionFactory.NewNotFoundOrBadRequestException(conventionRouteResult, directRouteResult);
        }
 internal abstract ControllerSelectionResult SelectController(ApiVersionControllerAggregator aggregator);