コード例 #1
0
            private static HttpResponseMessage CreateBadRequestResponse( HttpControllerContext controllerContext )
            {
                Contract.Requires( controllerContext != null );
                Contract.Ensures( Contract.Result<HttpResponseMessage>() != null );

                var request = controllerContext.Request;
                var exceptionFactory = new HttpResponseExceptionFactory( request );
                return exceptionFactory.CreateBadRequestResponseForUnsupportedApiVersion( request.GetRequestedApiVersion() );
            }
コード例 #2
0
            static HttpResponseMessage CreateBadRequestResponse(HttpControllerContext controllerContext)
            {
                Contract.Requires(controllerContext != null);
                Contract.Ensures(Contract.Result <HttpResponseMessage>() != null);

                var request          = controllerContext.Request;
                var model            = new Lazy <ApiVersionModel>(controllerContext.ControllerDescriptor.GetApiVersionModel);
                var exceptionFactory = new HttpResponseExceptionFactory(request, model);

                return(exceptionFactory.CreateBadRequestResponse(request.GetRequestedApiVersion()));
            }
コード例 #3
0
            private CandidateAction[] GetInitialCandidateList(HttpControllerContext controllerContext, bool ignoreVerbs = false)
            {
                Contract.Requires(controllerContext != null);
                Contract.Ensures(Contract.Result <CandidateAction[]>() != null);

                var actionName     = default(string);
                var incomingMethod = controllerContext.Request.Method;
                var routeData      = controllerContext.RouteData;
                var candidates     = default(CandidateAction[]);

                if (routeData.Values.TryGetValue(RouteValueKeys.Action, out actionName))
                {
                    var actionsFoundByName = standardActions.StandardActionNameMapping[actionName].ToArray();

                    if (actionsFoundByName.Length == 0)
                    {
                        var request          = controllerContext.Request;
                        var versionNeutral   = controllerContext.ControllerDescriptor.GetApiVersionModel().IsApiVersionNeutral;
                        var exceptionFactory = new HttpResponseExceptionFactory(request);

                        throw exceptionFactory.NewMethodNotAllowedException(versionNeutral, allowedMethods);
                    }

                    var candidatesFoundByName = new CandidateAction[actionsFoundByName.Length];

                    for (var i = 0; i < actionsFoundByName.Length; i++)
                    {
                        candidatesFoundByName[i] = new CandidateAction(actionsFoundByName[i]);
                    }

                    if (ignoreVerbs)
                    {
                        candidates = candidatesFoundByName;
                    }
                    else
                    {
                        candidates = FilterIncompatibleVerbs(incomingMethod, candidatesFoundByName);
                    }
                }
                else
                {
                    if (ignoreVerbs)
                    {
                        candidates = standardActions.StandardCandidateActions;
                    }
                    else
                    {
                        candidates = FindActionsForVerb(incomingMethod, standardActions.CacheListVerbs, standardActions.StandardCandidateActions);
                    }
                }

                return(candidates);
            }
コード例 #4
0
            HttpResponseMessage CreateSelectionError(HttpControllerContext controllerContext)
            {
                var actionsFoundByParams = FindMatchingActions(controllerContext, ignoreVerbs: true);

                if (actionsFoundByParams.Count == 0)
                {
                    return(CreateActionNotFoundResponse(controllerContext));
                }

                var request          = controllerContext.Request;
                var model            = controllerContext.ControllerDescriptor.GetApiVersionModel();
                var exceptionFactory = new HttpResponseExceptionFactory(request, new Lazy <ApiVersionModel>(() => model));

                return(exceptionFactory.CreateMethodNotAllowedResponse(model.IsApiVersionNeutral, allowedMethods));
            }
コード例 #5
0
            HttpResponseMessage CreateSelectionError(HttpControllerContext controllerContext)
            {
                Contract.Ensures(Contract.Result <HttpResponseMessage>() != null);

                var actionsFoundByParams = FindMatchingActions(controllerContext, ignoreVerbs: true);

                if (actionsFoundByParams.Count == 0)
                {
                    return(CreateActionNotFoundResponse(controllerContext));
                }

                var request          = controllerContext.Request;
                var versionNeutral   = controllerContext.ControllerDescriptor.GetApiVersionModel().IsApiVersionNeutral;
                var exceptionFactory = new HttpResponseExceptionFactory(request);

                return(exceptionFactory.CreateMethodNotAllowedResponse(versionNeutral, allowedMethods));
            }
コード例 #6
0
        protected virtual HttpActionDescriptor SelectActionVersion(HttpControllerContext controllerContext, IReadOnlyList <HttpActionDescriptor> candidateActions)
        {
            Arg.NotNull(controllerContext, nameof(controllerContext));
            Arg.NotNull(candidateActions, nameof(candidateActions));

            if (candidateActions.Count == 0)
            {
                return(null);
            }

            var request          = controllerContext.Request;
            var requestedVersion = request.GetRequestedApiVersion();
            var model            = new Lazy <ApiVersionModel>(controllerContext.ControllerDescriptor.GetApiVersionModel);
            var exceptionFactory = new HttpResponseExceptionFactory(request, model);

            if (candidateActions.Count == 1)
            {
                var action   = candidateActions[0];
                var versions = action.GetApiVersions();
                var matched  = versions.Count == 0 || versions.Contains(requestedVersion);
                return(matched ? action : null);
            }

            var implicitMatches = new List <HttpActionDescriptor>();
            var explicitMatches = new List <HttpActionDescriptor>();

            foreach (var action in candidateActions)
            {
                var versions = action.GetApiVersions();

                if (versions.Count == 0)
                {
                    implicitMatches.Add(action);
                }
                else if (versions.Contains(requestedVersion))
                {
                    explicitMatches.Add(action);
                }
            }

            switch (explicitMatches.Count)
            {
            case 0:
                switch (implicitMatches.Count)
                {
                case 0:
                    break;

                case 1:
                    return(implicitMatches[0]);

                default:
                    throw CreateAmbiguousActionException(implicitMatches);
                }

                break;

            case 1:
                return(explicitMatches[0]);

            default:
                throw CreateAmbiguousActionException(explicitMatches);
            }

            return(null);
        }