protected void PrepareInvalidResult <T>(ValidateableResponse <T> result) where T : class { // The ModelState may be valid at this point, BUT the result has validation errors (possibly from the Business layer) if (!result.IsValidResponse && ModelState.IsValid) { result.AddToModelState(ModelState); } }
public static void AddToModelState(this ValidateableResponse source, ModelStateDictionary modelState) { foreach (var error in source.Errors) { foreach (var item in error.Value) { modelState.AddModelError(error.Key, item); } } }
public static IActionResult ToUnprocessableRequestResult(this ValidateableResponse source, string message = null) { var outcome = OperationOutcome.ValidationFailOutcome(source.Errors, message ?? string.Empty); var apiResponse = new ApiResponse <UnprocessableEntityPayload> { Data = null, Outcome = outcome }; return(new ObjectResult(apiResponse) { StatusCode = StatusCodes.Status422UnprocessableEntity }); }