public override void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { var errorList = ModelStateFormatter.FormatErrors(context.ModelState); context.Result = new BadRequestObjectResult(errorList); } base.OnActionExecuting(context); }
public void Formatter_Should_Format_Single_Error_Correctly() { var modelState = new ModelStateDictionary(); modelState.AddModelError("", "This is an error message"); var list = new List <string>(); list.Add("This is an error message"); var formattedMessage = ModelStateFormatter.FormatErrors(modelState); formattedMessage .Should() .BeEquivalentTo(list); }
public void Formatter_Should_Format_Multiple_Errors_Correctly() { var modelState = new ModelStateDictionary(); var list = new List <string>(); var baseMessage = "This is error"; for (int i = 0; i < 10; i++) { var fullMessage = $"{baseMessage} {i}"; list.Add(fullMessage); modelState.AddModelError("", fullMessage); } var formattedMessage = ModelStateFormatter.FormatErrors(modelState); formattedMessage .Should() .BeEquivalentTo(list); }
public async Task <IActionResult> DeleteTak([FromRoute] int id) { var tak = await _takRepository.FindByIdAsync(id); if (tak == null) { return(NotFound($"Tak met id {id} werd niet gevonden")); } if (tak.Leiding.Count > 0) { ModelState.AddModelError("LeidingAanwezig", "De tak bevat nog leiding."); return(BadRequest(ModelStateFormatter.FormatErrors(ModelState))); } _takRepository.Delete(tak); await _takRepository.SaveChangesAsync(); var model = _mapper.Map <BasicSectionDTO>(tak); return(Ok(model)); }