コード例 #1
0
    public async Task ObjectResult_ExecuteResultAsync_ResultAndProblemDetailsHaveStatusCodes()
    {
        // Arrange
        var details = new ProblemDetails {
            Status = StatusCodes.Status422UnprocessableEntity,
        };

        var result = new BadRequestObjectResult(details)
        {
            Formatters = new FormatterCollection <IOutputFormatter>()
            {
                new NoOpOutputFormatter(),
            },
        };

        var actionContext = new ActionContext()
        {
            HttpContext = new DefaultHttpContext()
            {
                RequestServices = CreateServices(),
            }
        };

        // Act
        await result.ExecuteResultAsync(actionContext);

        // Assert
        Assert.Equal(StatusCodes.Status422UnprocessableEntity, details.Status.Value);
        Assert.Equal(StatusCodes.Status400BadRequest, result.StatusCode.Value);
        Assert.Equal(StatusCodes.Status400BadRequest, actionContext.HttpContext.Response.StatusCode);
    }
コード例 #2
0
        /// <summary>
        /// Responds to the request and executes the action.
        /// </summary>
        /// <param name="statusCode">The status code.</param>
        /// <param name="error">The error.</param>
        /// <returns>HttpResponseMessage.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public static Task ExecuteWithAsync(this ActionContext actionContext, int statusCode, SerializableError error)
        {
            if (error == null)
            {
                throw new ArgumentNullException("error");
            }

            var badness = new BadRequestObjectResult(error)
            {
                StatusCode = (int)statusCode
            };

            return(badness.ExecuteResultAsync(actionContext));
        }
コード例 #3
0
        /// <summary>
        /// Responds to the request and executes the action.
        /// </summary>
        /// <param name="statusCode">The status code.</param>
        /// <param name="modelState">State of the model.</param>
        /// <returns>HttpResponseMessage.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public static Task ExecuteWithAsync(this ActionContext actionContext, int statusCode, ModelStateDictionary modelState)
        {
            if (modelState == null)
            {
                throw new ArgumentNullException("modelState");
            }

            var badness = new BadRequestObjectResult(modelState)
            {
                StatusCode = (int)statusCode
            };

            return(badness.ExecuteResultAsync(actionContext));
        }
コード例 #4
0
        public static Task ProblemAsync(this ActionContext context, Exception ex)
        {
            if (context.HttpContext.Features.Get <IHttpResponseFeature>().HasStarted)
            {
                // If already started writing response, do not rewrite
                // as this will throw an InvalidOperationException.
                return(Task.CompletedTask);
            }
            else
            {
                ActionResult result = new BadRequestObjectResult(ex.ToProblemDetails((int)HttpStatusCode.BadRequest));

                return(result.ExecuteResultAsync(context));
            }
        }
コード例 #5
0
 public static Task ProblemAsync(this ActionContext context, BadRequestObjectResult result)
 {
     if (context.HttpContext.Features.Get <IHttpResponseFeature>().HasStarted)
     {
         // If already started writing response, do not rewrite
         // as this will throw an InvalidOperationException.
         return(Task.CompletedTask);
     }
     else
     {
         // Need to manually add this here because this ObjectResult is not processed by the filter pipeline,
         // which would look up the applicable content types and apply them before executing the result.
         result.ContentTypes.Add(ContentTypes.ApplicationProblemJson);
         return(result.ExecuteResultAsync(context));
     }
 }
コード例 #6
0
 public async Task Invoke(HttpContext context)
 {
     try
     {
         await this.next(context);
     }
     catch (EntityNotFoundException ex)
     {
         var response = new NotFoundObjectResult(ex.Message);
         await response.ExecuteResultAsync(
             new ActionContext(context, context.GetRouteData(), new ActionDescriptor()));
     }
     catch (EntityValidationException ex)
     {
         var response = new BadRequestObjectResult($"{ex.Message}, {string.Join(", ", ex.Errors.Select(x => x.ErrorMessage))}");
         await response.ExecuteResultAsync(
             new ActionContext(context, context.GetRouteData(), new ActionDescriptor()));
     }
 }
コード例 #7
0
        public static Task ProblemAsync(this ActionContext context, Exception ex)
        {
            ActionResult result = new BadRequestObjectResult(ex.ToProblemDetails((int)HttpStatusCode.BadRequest));

            return(result.ExecuteResultAsync(context));
        }