This example shows how to return a custom IActionResult when the model binding validation fails:
builder.Services.AddControllers().ConfigureApiBehaviorOptions((options) =>
{
options.InvalidModelStateResponseFactory = (actionContext) =>
{
return new BadRequestObjectResult(new
{
Error = actionContext.ModelState.First().Value?.Errors.First().ErrorMessage
});
};
});
then, if you use axios for the API call:
try {
const result = await axios.post(`......`, {.....});
} catch (e) {
console.dir(e.response.data.error);
}
Tags
ASP .NET core