Search code examples
c#asp.net-coreaction-filter

Access ActionArguments in IExceptionFilter


Is it possible to access ActionArguments in an IExceptionFilter?

enter image description here

According to the above picutre Exception Filters run after Action Filters which have access to them. So I don't see why they can't.

In an ActionFilter you access it like this

public class MyFilter : IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext context)
    {
        context.ActionArguments // I need access to this in order to find out what type the input argument(s) was.
    }
}

But an IExceptionFilter uses public void OnException(ExceptionContext context) instead.


Solution

  • I figured it out :)

    public class ExceptionFilter : IExceptionFilter, IActionFilter
    {
        private IDictionary<string, object> _actionArguments;
    
        public void OnException(ExceptionContext context)
        {
            _actionArguments // great success!
        }
    
        public void OnActionExecuting(ActionExecutingContext context)
        {
            _actionArguments = context.ActionArguments;
        }
    
        public void OnActionExecuted(ActionExecutedContext context)
        {
            // do nothing
        }
    }