I'm currently creating an ASP Web API and I found out that if requests are done to the API that contain empty values, that I receive "ugly" error exceptions in the ModelState that I don't want to show to my users. Let's say my request body looks like this:
{"Id": "", "Name": "", "Description": "", "Created": "", "UserId": "", "AmountOfUsers": "", "MinimumAmountOfUsers": "", "Location": "", "Activated": "", "CategoryId": "", "Date": ""}
Then I will receive the following ModelState exception for Id:
Error converting value {null} to type 'System.Int32'. Path 'Id', line 1, position 9
The same goes for all other value types.
I do have a [Required]
data annotation (with custom error messages) for those fields in my model, but that does not seem to do the trick.
I am now wondering what the best solution is to resolve this. All the models that users post/put are view models, so I was thinking about making all value types nullable and then cast them afterwards when I map them to database models. I am unsure whether there are better solutions though and therefore I would love to hear you opinions.