question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Web Api Action Filters Do Not Run

See original GitHub issue

FluentValidation: 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:

  1. The MyActionFilter.ctor is successfully called.
  2. Neither of the OnAction* methods are called.
  3. 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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Mar 16, 2021

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 use ApiController)

1reaction
JeremySkinnercommented, Mar 16, 2021

Thought that was a requirement in creating Web Apis

No, it isn’t required. It’s a shortcut that enables a certain set of conventions.

Is there a work around so that filters continue to execute?

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.

Read more comments on GitHub >

github_iconTop 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 >
Filters in ASP.NET Core
Learn how filters work and how to use them in ASP.NET Core.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found