How to read arguments from ActionExecutedContext in ASP.NET Core ActionFilter

43 views Asked by At

It is easy to get argument in

OnActionExecuting(ActionExecutingContext context)
context.ActionArguments

But how to do it in

public override void OnActionExecuted(ActionExecutedContext context)

Thanks!

There is no property ActionArguments in ActionExecutedContext

1

There are 1 answers

0
Rena On

You can store the action arguments from HttpContext.Items and then get it in OnActionExecuted:

public class CustomFilter : IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext context)
    {
        // Retrieve action arguments from HttpContext.Items
        var actionArguments = context.HttpContext.Items["ActionArguments"] as IDictionary<string, object>;

    }
    public void OnActionExecuting(ActionExecutingContext context)
    {
        // Store action arguments in HttpContext
        context.HttpContext.Items["ActionArguments"] = context.ActionArguments;

    }
}

Note: since HttpContext.Items persists for the entirety of the request, ensure that you use unique keys to store your data to avoid any conflicts.