Just to get a list of the error messages for each Model Property that failed validation in the manner you want as indicated above using an extension method. i.e ModelState.ToJson()
, you need create a static class with a static function ToJson(...)
. The code example will look something like this.
public static class ModelStateExtensions
{
/// <summary>
/// Reads all the error messages in a <see cref="ModelStateDictionary"/> as
/// a collection and returns a JSON <see cref="string"/> of the list.
/// </summary>
/// <param name="modelstate">Current modelstate assuming that you've checked
/// and confirmed that is Invalid using <see
/// cref="ModelStateDictionary.IsValid"/>
/// </param>
/// <returns>
/// Collection of validation errors for the model as a JSON string.
/// </returns>
public static string ToJson(this ModelStateDictionary modelstate)
{
List<string> errors = modelstate.Values
.SelectMany(x => x.Errors)
.Select(x => x.ErrorMessage)
.ToList();
return JsonConvert.SerializeObject(errors);
}
}
The ModelState
property on every controller is normally a ModelStateDictionary
, so if we want an additional method for it, that is the class we need to extend. You can learn more about extension methods in C# by following this Link.
Let now see how to use our extension method in a sample controller action:
public IActionResult Create(UserViewModel model)
{
if(!ModelState.IsValid)
{
string json = ModelState.ToJson();
// insert code to log json to file here
return BadRequest(ModelState);
}
}