© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
25 replies
TempleOS

❔ OnActionExecuted exception filter runs before exception is thrown and is never called

I'm having a bit of an issue where I'm following the guide from https://learn.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-7.0#use-exceptions-to-modify-the-response

The goal is to modify the status code when an exception is thrown, I know there are alternative ways to do this, but this is the most straightforward.

I already have an expected exception, I built the filter, then deployed it onto
builder.Services.AddControllers
builder.Services.AddControllers
options.Filters
options.Filters
, but it doesn't quite work as expected.

When using the debugger I noticed that the onActionExecuted is running before the exception is thrown. The exception then continues, gets thrown, but then the status code is never changed.

This was how my original implementation was supposed to behave, but after just trying to copy/paste the exact example from the docs, it didn't work either.

Could someone lead me in the right direction on figuring out why OnActionExecuted is not running for when the exception is thrown?

There is one thing to keep in mind, the app is very monorepo like, with many sub-repositories. The startup is in
/api
/api
& the exception and exception filter are declared, but used in
/services
/services
. But I don't know why this would cause any issues.
Handle errors in ASP.NET Core web APIs
Learn about error handling with ASP.NET Core web APIs.
Handle errors in ASP.NET Core web APIs
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ httpclient not sending request and no exception is thrown
C#CC# / help
4y ago
Getting call stack that was a thing before exception was thrown
C#CC# / help
3y ago
✅ Exception thrown: 'System.FormatException' in mscorlib.dll
C#CC# / help
2y ago
Exception thrown: 'System.BadImageFormatException' in BruhCode2.dllAn unhandled exception of type '
C#CC# / help
3y ago