Search code examples
asp.net-mvcasp.net-web-apiaction-filter

Get Action arguments similar to webapi in mvc action filter


I have an API method that accept a Dictionary<string, object> that is returned from HttpActionExecutedContext.ActionContext.ActionArguments property (used within a WebApi action filter). I need to construct a similar dictionary from ActionExecutedContext in a MVC action filter so that I can use same API call from the mvc filter as well.

I tried several approaches but they are not very straitforward. If there is easy way to construct the actionArguments dictionary from ActionExecutedContext please let me know.

    //webapi - working fine
    public override void OnActionExecuted(HttpActionExecutedContext context)
    {
        Call_A(context.ActionContext.ActionArguments)        
    }

    //mvc- need to find a way to get argument dictionary
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        Call_A(context.?)        
    }

Solution

  • Not sure if this exists for OnActionExecuted, but it definitely exists for OnActionExecuting. If you do need it on Executed (if it doesn't exist, I don't think it does) then you can store it in the filter temporarily, eg:

    public IDictionary<string, object> actionParams { get; set; }
    
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        this.actionParams = context.ActionParameters;
    }
    
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        foreach (var param in this.actionParams) {
            // param.Key
            // param.Value
        }
    }