C
C#12mo ago
Nickolaki

❔ Change default behaviour of .NET Web API Validation Errors

Hi does anyone know how I can override the default problem details/validation error response .net gives when things like Guid and DateTime's are in an incorrect format?
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-9f2acabaefdccec268c6f8bc2594d0ba-bdd955faf7e6202a-00",
"errors": {
"command": [
"The command field is required."
],
"$.boardId": [
"The JSON value could not be converted to System.Guid. Path: $.boardId | LineNumber: 0 | BytePositionInLine: 30."
]
}
}
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-9f2acabaefdccec268c6f8bc2594d0ba-bdd955faf7e6202a-00",
"errors": {
"command": [
"The command field is required."
],
"$.boardId": [
"The JSON value could not be converted to System.Guid. Path: $.boardId | LineNumber: 0 | BytePositionInLine: 30."
]
}
}
I have implemented my own validation error responses but can't seem to avoid these type of ones firing^ Mine look like this:
{
"status": 422,
"message": "Validation errors occurred",
"errors": [
{
"property": "BoardId",
"message": "Board Id is required."
}
]
}
{
"status": 422,
"message": "Validation errors occurred",
"errors": [
{
"property": "BoardId",
"message": "Board Id is required."
}
]
}
FYI: my validation logic is driven by FluentValidation, I'm not using attributes etc.
3 Replies
blueberriesiftheywerecats
Custom Model Binding in ASP.NET Core
Learn how model binding allows controller actions to work directly with model types in ASP.NET Core.
blueberriesiftheywerecats
Stack Overflow
ASP.NET Core customize the error when model binding fails
I try to apply back end validation whenever empty or invalid values are send to ASP.NET Core Web API endpoint, but I can't figure out how to handle model binding failure errors. Getting this error
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.