{
"errors": {
"price": [
"Could not convert string to decimal: dasdfasdf. Path 'price', line 3, position 22."
],
"userId": [
"Could not convert string to integer: hsad. Path 'userId', line 6, position 27."
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|bcaa98d957e1c04181000489a0bc4950.9753735a_"
}
I tried custom model binder, but I need this for specific properties in a model. I also tried the JsonConvert with the property but couldn't find a way to inject the error message in the model state
You can customize the ApiController's ModelState validator response by configuring InvalidModelStateResponseFactory
of ApiBehaviorOptions
like this:
services.Configure<ApiBehaviorOptions>(options =>
{
options.InvalidModelStateResponseFactory = actionContext =>
{
var errors = actionContext.ModelState
.Where(e => e.Value.Errors.Count > 0)
.Select(e => new Error
{
Name = e.Key,
Message = e.Value.Errors.First().ErrorMessage
}).ToArray();
return new BadRequestObjectResult(errors);
}
});
Also, you can read more about ApiController behaviors here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.