How to disable ModelStateInvalidFilter in ASP.NET Core
By FoxLearn 2/4/2025 8:34:14 AM 8
If you want to disable this automatic behavior, you can configure it globally or locally in your controllers.
Why Disable ModelStateInvalidFilter?
Disabling the ModelStateInvalidFilter
gives you the flexibility to manually perform model validation in your application, allowing you to handle the validation errors in a custom way. This can be useful if you need more control over the error handling process or if you want to return a custom error response.
1. Disable ModelStateInvalidFilter Globally
If you want to disable ModelStateInvalidFilter
for all actions in your API, you can do so by configuring the ApiBehaviorOptions
in the ConfigureServices
method. By setting SuppressModelStateInvalidFilter
to true
, you will globally disable the filter.
public void ConfigureServices(IServiceCollection services) { services.AddControllers().ConfigureApiBehaviorOptions(options => { // Disable the ModelStateInvalidFilter globally options.SuppressModelStateInvalidFilter = true; }); }
Once this is done, the framework will no longer automatically return a 400 Bad Request
when the model validation fails. Instead, you'll need to manually check the ModelState
in your controller actions.
2. Disable ModelStateInvalidFilter for Specific Actions
If you only want to disable the filter for specific actions, you can implement a custom IActionModelConvention
attribute. This approach allows you to remove the ModelStateInvalidFilter
only from the actions where you want to disable it.
Step 1: Implement the IActionModelConvention
Attribute
First, you need to create a custom attribute that implements IActionModelConvention
to remove the ModelStateInvalidFilter
from the action filters.
using Microsoft.AspNetCore.Mvc.ApplicationModels; [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public class DisableInvalidModelFilterAttribute : Attribute, IActionModelConvention { private const string FilterName = "ModelStateInvalidFilterFactory"; public void Apply(ActionModel action) { // Search for the action filter and remove it for (var i = 0; i < action.Filters.Count; i++) { if (action.Filters[i].GetType().Name == FilterName) { action.Filters.RemoveAt(i); return; } } } }
This custom attribute looks for the ModelStateInvalidFilterFactory
filter and removes it from the list of filters for the action.
Step 2: Apply the Attribute to Specific Actions
After you’ve created the DisableInvalidModelFilterAttribute
, you can apply it to the actions where you want to disable the ModelStateInvalidFilter
.
[ApiController] [Route("[controller]")] public class StocksController : ControllerBase { [HttpPost] [DisableInvalidModelFilter] // Disable the ModelStateInvalidFilter for this action public IActionResult Post(StockOrder stockOrder) { // Manually validate the model state or perform other logic if (!ModelState.IsValid) { return BadRequest(ModelState); // You can handle the error here } return Ok(stockOrder); } }
With this approach, the ModelStateInvalidFilter
will be disabled only for the Post
action of the StocksController
. The other actions in your controller will still use the default model validation behavior.
Conventions Only Run Once
You might be wondering if adding the IActionModelConvention
to your actions will slow down your requests. The good news is that the framework only runs these conventions once during the application’s initialization. They don’t get executed with each incoming request, so performance will not be impacted.
Important Notes
Cannot Remove Custom Filters: This approach works only for built-in filters like ModelStateInvalidFilter
. If you’ve added custom action filters to your application, this method will not work for removing them. The reason is that custom action filters aren’t available in Action.Filters
during the IActionModelConvention
execution phase.
Disabling the ModelStateInvalidFilter
in ASP.NET Core gives you full control over model validation handling. You can choose to disable it globally for your entire application or selectively disable it for specific actions.
- How to supply IOptions in ASP.NET Core
- Logging requests and responses in ASP.NET Core
- How to manually validate a model in a controller in ASP.NET Core
- How to add custom middleware in ASP.NET Core
- How to Turn Off Startup Logging in ASP.NET Core
- Dependency inject BackgroundService into controllers
- How to Configure JSON Serializer options in ASP.NET Core
- How to use Razor View Engine in ASP.NET Core