コード例 #1
0
        HttpResponseException?CreateBadRequest(ControllerSelectionResult conventionRouteResult, ControllerSelectionResult?directRouteResult)
        {
            ApiVersion?requestedVersion;

            if (conventionRouteResult.CouldMatchVersion)
            {
                requestedVersion = conventionRouteResult.RequestedVersion;
            }
            else if (directRouteResult != null && directRouteResult.CouldMatchVersion)
            {
                requestedVersion = directRouteResult.RequestedVersion;
            }
            else
            {
                return(null);
            }

            return(CreateBadRequest(requestedVersion));
        }
コード例 #2
0
        HttpResponseException CreateNotFound(ControllerSelectionResult conventionRouteResult)
        {
            Contract.Requires(conventionRouteResult != null);

            var message       = SR.ResourceNotFound.FormatDefault(request.RequestUri);
            var messageDetail = default(string);

            if (IsNullOrEmpty(conventionRouteResult.ControllerName))
            {
                messageDetail = SR.ControllerNameNotFound.FormatDefault(request.RequestUri);
            }
            else
            {
                messageDetail = SR.DefaultControllerFactory_ControllerNameNotFound.FormatDefault(conventionRouteResult.ControllerName);
            }

            TraceWriter.Info(request, ControllerSelectorCategory, message);

            return(new HttpResponseException(request.CreateErrorResponse(NotFound, message, messageDetail)));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        HttpResponseException CreateBadRequest(ControllerSelectionResult conventionRouteResult, ControllerSelectionResult directRouteResult)
        {
            Contract.Requires(conventionRouteResult != null);

            var requestedVersion = default(ApiVersion);

            if (conventionRouteResult.CouldMatchVersion)
            {
                requestedVersion = conventionRouteResult.RequestedVersion;
            }
            else if (directRouteResult != null && directRouteResult.CouldMatchVersion)
            {
                requestedVersion = directRouteResult.RequestedVersion;
            }
            else
            {
                return(null);
            }

            return(CreateBadRequest(requestedVersion));
        }
コード例 #5
0
        HttpResponseException CreateNotFound(ControllerSelectionResult conventionRouteResult)
        {
            var    safeUrl = request.RequestUri.SafeFullPath();
            var    message = SR.ResourceNotFound.FormatDefault(safeUrl);
            string messageDetail;

            if (IsNullOrEmpty(conventionRouteResult.ControllerName))
            {
                messageDetail = SR.ControllerNameNotFound.FormatDefault(safeUrl);
            }
            else
            {
                messageDetail = SR.DefaultControllerFactory_ControllerNameNotFound.FormatDefault(conventionRouteResult.ControllerName);
            }

            TraceWriter.Info(request, ControllerSelectorCategory, message);

#pragma warning disable CA2000 // Dispose objects before losing scope
            return(new HttpResponseException(request.CreateErrorResponse(NotFound, message, messageDetail)));

#pragma warning restore CA2000 // Dispose objects before losing scope
        }
コード例 #6
0
 internal HttpResponseException NewNotFoundOrBadRequestException(ControllerSelectionResult conventionRouteResult, ControllerSelectionResult directRouteResult) =>
 CreateBadRequest(conventionRouteResult, directRouteResult) ?? CreateNotFound(conventionRouteResult);
 internal HttpResponseException NewNotFoundOrBadRequestException(ControllerSelectionResult conventionRouteResult, ControllerSelectionResult directRouteResult) =>
 CreateBadRequestForUnsupportedApiVersion(conventionRouteResult, directRouteResult) ?? CreateBadRequestForInvalidApiVersion() ?? CreateNotFound(conventionRouteResult);