I would like to indicate an error code that will be returned from my API along with an error message if the validation fails. The goal is to specify a property check attribute, for example:
[Range(1, int.MaxValue, ErrorMessage = "Page must be 1 or greater", ErrorCode = 1234)]
And in case someone requests page 0, return 400 Bad Request with a JSON error object in the bodyfollowing way:
{
"errorCode": 1234,
"errorMessage": "Page : Page must be 1 or greater"
}
I already have a custom ModelValidationFilterone that returns an array of error messages from ModelStateDictionary, so everything works, but I don’t see a direct way to get the error code included there without overriding all the validation classes of the MVC model.
This seems like a problem that at some point had to be solved by someone, but I cannot find anything to support this theory.
source
share