in Asp Net Core, it seems that by default it's not enforce to check whether or not the model is valid. As such, I have to write something like this at the start of every controller:
if (!modelState.IsValid)
{
var errorText = "";
foreach (var value in ModelState.Values)
{
foreach (var error in value.Errors)
{
if (error.ErrorMessage != null)
{
errorText += error.ErrorMessage + "\n";
}
if (error.Exception.Message != null)
{
errorText += error.Exception.Message + "\n";
}
}
}
return StatusCode(400, errorText);
}
Is there a way to automatically do this for every controller I decide ? I know there is [ApiController] tag but AFAIK it doesn't allow doing this much "logic" for the error message and return code
thanks