Example #1
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);
        }
Example #2
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);
        }