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); }
/// <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)); }
/// <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)); }
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)); } }
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)); } }
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())); } }
public static Task ProblemAsync(this ActionContext context, Exception ex) { ActionResult result = new BadRequestObjectResult(ex.ToProblemDetails((int)HttpStatusCode.BadRequest)); return(result.ExecuteResultAsync(context)); }