I wrote web API in .NET Framework. I made a global error filter.
public class UnhandledExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
//log somewhere
//Make user friendly error message
}
}
And I added it to global filters
config.Filters.Add(new UnhandledExceptionFilter());
This works fine when an error occurs in the controller. But it does not work when an error occurs in default model binding.
Let's say I have one action and it gets one DateTime parameter.
public class MyController: ApiController
{
public override void CheckBirthdate(DateTime birthdate)
{
throw new Exception("Test");
}
}
If I call MyController/CheckBirthdate?birthdate=03/05/2021 like that my global error handling works. But I call MyController/CheckBirthdate?birthdate=test like that my global error handling not works. I get back error like that
Could not convert string to DateTime: test. Path 'birthdate'
Why global error handling filter can not catch it? How can I solve it?.