/// <summary>
        /// Verifies the requested API version is not ambiguous.
        /// </summary>
        /// <param name="context">The current <see cref="RouteContext">route context</see>.</param>
        /// <param name="apiVersion">The requested <see cref="ApiVersion">API version</see> or <c>null</c>.</param>
        /// <returns>True if the requested API version is ambiguous; otherwise, false.</returns>
        /// <remarks>This method will also change the <see cref="RouteContext.Handler"/> to an appropriate
        /// error response if the API version is ambiguous.</remarks>
        protected virtual bool IsRequestedApiVersionAmbiguous(RouteContext context, out ApiVersion?apiVersion)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var httpContext = context.HttpContext;

            try
            {
                apiVersion = httpContext.GetRequestedApiVersion();
            }
            catch (AmbiguousApiVersionException ex)
            {
                Logger.LogInformation(ex.Message);
                apiVersion = default;

                var handlerContext = new RequestHandlerContext(Options.ErrorResponses)
                {
                    Code    = AmbiguousApiVersion,
                    Message = ex.Message,
                };

                context.SetHandlerOrEndpoint(new BadRequestHandler(handlerContext));
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Verifies the requested API version is not ambiguous.
        /// </summary>
        /// <param name="context">The current <see cref="RouteContext">route context</see>.</param>
        /// <param name="apiVersion">The requested <see cref="ApiVersion">API version</see> or <c>null</c>.</param>
        /// <returns>True if the requested API version is ambiguous; otherwise, false.</returns>
        /// <remarks>This method will also change the <see cref="RouteContext.Handler"/> to an appropriate
        /// error response if the API version is ambiguous.</remarks>
        protected virtual bool IsRequestedApiVersionAmbiguous(RouteContext context, out ApiVersion apiVersion)
        {
            Arg.NotNull(context, nameof(context));

            var httpContext = context.HttpContext;

            try
            {
                apiVersion = httpContext.GetRequestedApiVersion();
            }
            catch (AmbiguousApiVersionException ex)
            {
                Logger.LogInformation(ex.Message);
                apiVersion = default;

                var handlerContext = new RequestHandlerContext(Options.ErrorResponses)
                {
                    Code    = AmbiguousApiVersion,
                    Message = ex.Message,
                };

                context.Handler = new BadRequestHandler(handlerContext);
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Verifies the requested API version is not ambiguous.
        /// </summary>
        /// <param name="context">The current <see cref="RouteContext">route context</see>.</param>
        /// <param name="apiVersion">The requested <see cref="ApiVersion">API version</see> or <c>null</c>.</param>
        /// <returns>True if the requested API version is ambiguous; otherwise, false.</returns>
        /// <remarks>This method will also change the <see cref="RouteContext.Handler"/> to an appropriate
        /// error response if the API version is ambiguous.</remarks>
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
        protected virtual bool IsRequestedApiVersionAmbiguous(RouteContext context, out ApiVersion?apiVersion)
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var httpContext = context.HttpContext;

            try
            {
                apiVersion = httpContext.GetRequestedApiVersion();
            }
            catch (AmbiguousApiVersionException ex)
            {
                Logger.LogInformation(ex.Message);
                apiVersion = default;

                var handlerContext = new RequestHandlerContext(Options.ErrorResponses)
                {
                    Code    = AmbiguousApiVersion,
                    Message = ex.Message,
                };

                context.Handler = new BadRequestHandler(handlerContext);
                return(true);
            }

            return(false);
        }
Example #4
0
        RequestHandler VerifyRequestedApiVersionIsNotAmbiguous(HttpContext httpContext, out ApiVersion apiVersion)
        {
            Contract.Requires(httpContext != null);

            try
            {
                apiVersion = httpContext.GetRequestedApiVersion();
            }
            catch (AmbiguousApiVersionException ex)
            {
                Logger.LogInformation(ex.Message);
                apiVersion = default(ApiVersion);
                var handlerContext = new RequestHandlerContext(Options.ErrorResponses)
                {
                    Code    = AmbiguousApiVersion,
                    Message = ex.Message,
                };
                return(new BadRequestHandler(handlerContext));
            }

            return(null);
        }
 internal BadRequestHandler(RequestHandlerContext context) : base(context)
 {
 }
Example #6
0
 protected RequestHandler(RequestHandlerContext context) => Context = context;
 internal MethodNotAllowedHandler(RequestHandlerContext context) : base(context)
 {
 }