コード例 #1
0
 public void ActionForDataAnnotationLocalization(AnnotatedExampleModel model)
 {
     if (!ModelState.IsValid)
     {
         var key = nameof(AnnotatedExampleModel.Value);
         foreach (var error in ModelState[key].Errors)
         {
             _logger.LogInformation($"Localized validation error message: {error.ErrorMessage}");
         }
     }
 }
コード例 #2
0
        public IActionResult SetExampleWithInputModel(AnnotatedExampleModel model)
        {
            if (!ModelState.IsValid)
            {
                foreach (var error in ModelState.SelectMany(kvp => kvp.Value.Errors.Select(err => new { kvp.Key, Message = err.ErrorMessage })))
                {
                    _logger.LogInformation($"Error at {error.Key}: {error.Message}");
                }
                return(BadRequest(ModelState));
            }

            return(NoContent());
        }