In ASP.NET Core with controllers I was able to customize the error response when a model binding error ocurred by adding the following in Program.cs:
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.InvalidModelStateResponseFactory = actionContext =>
{
var errors = actionContext.ModelState
.Where(e => e.Value.Errors.Count > 0)
.Select(e => new
{
Name = e.Key,
Message = e.Value.Errors.First().ErrorMessage
}).ToArray();
return new BadRequestObjectResult(errors);
};
});
This doesn't seem to be working with minimal API. How can I achieve the same results with Minimal API?