In an ASP.NET Core application, if you use AddMvc()
or AddControllers()
, you can configure the default behaviour in case the input DTO is invalid, like:
services.AddControllers()
.ConfigureApiBehaviorOptions(options => {
options.InvalidModelStateResponseFactory = ctx => {
return new UnprocessableEntityResult();
};
});
In this way, every time the mvc system spots that the input DTO results in an invalid ModelState, the error above is returned, without the need to do this in every single controller's method:
if (!ModelState.IsValid) {
return new UnprocessableEntityResult();
}
Is there a way to use the ModelState
or similar to centralize the management of ModelState errors or other concerns in ASP.NET Core Minimal API?
No, there is no ModelState
and alike for Minimal APIs out of the box. You will need to rollout your own one or use some 3rd party library:
System.ComponentModel.DataAnnotations.Validator
(for example like here) which can process corresponding attributes.FluentValidation
libraryMiniValidation
libraryYou can use it "inline" (i.e. create/resolve the validator and use it inside the Minimal API handler). For example for FluentValidation
:
app.MapPost("/person", async (IValidator<Person> validator, IPersonRepository repository, Person person) =>
{
ValidationResult validationResult = await validator.ValidateAsync(person);
if (!validationResult.IsValid)
{
return Results.ValidationProblem(validationResult.ToDictionary());
}
repository.Save(person);
return Results.Created($"/{person.Id}", person);
});
This of course can become cumbersome, but you can use Minimal API's filters to overcome this, either by rolling out your own one, like in this answer (note that you can roll out a "global" one using Route groups).
Or you can use another 3rd party library like O9d.AspNet.FluentValidation which implements this:
var group = app.MapGroup("/")
.WithValidationFilter();
group.MapPost("/person", async (IPersonRepository repository, Person person) =>
{
repository.Save(person);
return Results.Created($"/{person.Id}", person);
});