The [ApiController]
attribute that you’ve applied to your controller adds Automatic HTTP 400 Responses to the MVC pipeline, which means that your custom filter and action aren’t executed if ModelState
is invalid.
I see a few options for affecting how this works:
-
Remove the
[ApiController]
attributeAlthough you can just remove the
[ApiController]
attribute, this would also cause the loss of some of the other features it provides, such as Binding source parameter inference. -
Disable only the Automatic HTTP 400 Responses
Here’s an example from the docs that shows how to disable just this feature:
services.AddControllers() .ConfigureApiBehaviorOptions(options => { // ... options.SuppressModelStateInvalidFilter = true; // ... }
This code goes inside of your
Startup
‘sConfigureServices
method. -
Customise the automatic response that gets generated
If you just want to provide a custom response to the caller, you can customise what gets returned. I’ve already described how this works in another answer, here.