Web Api Action Filters Do Not Run
See original GitHub issueFluentValidation: 9.5.0 ASP.NET: 5.0
When I have my own action filters in a Web Api controller, if I am using FluentValidation, the filters do not execute.
Given the following filter:
public class MyActionFilter : IActionFilter
{
public MyActionFilter()
{
System.Diagnostics.Trace.WriteLine( "ctor" );
}
public void OnActionExecuting( ActionExecutingContext context )
{
System.Diagnostics.Trace.WriteLine( "OnActionExecuting" );
}
public void OnActionExecuted( ActionExecutedContext context )
{
System.Diagnostics.Trace.WriteLine( "OnActionExecuted" );
}
}
If I configure my builder as the following:
builder
.AddControllers()
.AddMvcOptions( opt =>
{
opt.Filters.Add( typeof( MyActionFilter ) );
} )
.AddFluentValidation( cfg =>
{
cfg.RegisterValidatorsFromAssemblyContaining<TValidatorsAssembly>();
} );
The following occurs:
- The MyActionFilter.ctor is successfully called.
- Neither of the
OnAction*
methods are called. - The web api result is a
ValidationProblemDetails
with correct FluentValidation error messages returned.
If I remove the call to AddFluentValidation
, then I do successfully hit the OnAction*
methods.
If I try this same pattern for my Razor Pages using an IPageFilter
(still using AddFluentValidation
), then the OnPageHandlerExecuting
is successfully called.
Is this a bug in FluentValidation or am I misunderstanding how the flow should work for web api actions?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Action Filter is not working in asp.net core Web API
I have created an action filter for HTTP request validation in ASP.NET Core 3.1 Web API. Unfortunately it is not working.
Read more >Implementing Action Filters in ASP.NET Core
In this article, we are going to talk about Action filters and how to use them to create cleaner and reusable code in...
Read more >ASP.NET Web API Filters
Web API includes filters to add extra logic before or after action method executes. Filters can be used to provide cross-cutting features such...
Read more >Avoid Duplicating Code by Using ASP.NET Core Filters
Filters run in their own filter pipeline (action invocation pipeline) after the ASP.NET Core selects the action to be executed. Probably you ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d also suggest reading up on how filters work in ASP.NET: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-5.0 The section on “Cancellation and short-circuiting” explains how one filter can be used to cancel the execution of all others (which is what the
ModelStateInvalidFilter
is doing - which is enabled when you useApiController
)No, it isn’t required. It’s a shortcut that enables a certain set of conventions.
You’d have to disable the automatic 400 responses and switch to returning them manually (or use a different filter type). The ASP.NET documentation explains how to do this. https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-5.0#disable-automatic-400-response I’d also suggest reading the rest of that page, as it explains exactly what the ApiController attribute does.
I’m going to close the issue now as it isn’t related to FluentValidation.