I created a new .Net 5 Web API with the following setup
public class MyController : ControllerBase
{
[HttpGet("{Title}")]
public ActionResult<string> Get([FromRoute] RouteModel routeModel)
{
return Ok(routeModel);
}
}
public class RouteModel
{
[MinLength(3)]
public string Title { get; set; }
}
The request validation works fine. Since I'm using FluentValidation I installed the package
FluentValidation.AspNetCore v10.3.0
and updated the Startup.ConfigureServices method to
public void ConfigureServices(IServiceCollection services)
{
services.AddValidatorsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication", Version = "v1" });
});
}
Instead of using the data annotations I removed the data annotations from RouteModel.Title and created this validator
public class RouteModelValidator : AbstractValidator<RouteModel>
{
public RouteModelValidator()
{
RuleFor(model => model.Title).MinimumLength(3);
}
}
I would expect a ValidationException and this would result into a 500 status code if the title has a length less than 3. But it seems there is no validation trigger anymore, the request always passes.
Does someone know what's missing?