MVC Action Filters using parameters passed to the for ActionResult?

前端 未结 2 2215
半阙折子戏
半阙折子戏 2021-02-19 20:03

I created a custom Action Filter with no problem.

But I would like to modify the Action Filter to use some of the parameters actually passed to my method.

So if

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-19 20:54

    You can try OnActionExecuting override, where you do have access to action parameters.

    public class MyAttribute: ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {    
            if (filterContext.ActionParameters.ContainsKey("userId"))
            {
                var userId = filterContext.ActionParameters["userId"] as Guid;
                if (userId != null)
                {
                    // Really?! Great!            
                }
            }
        }
    } 
    

提交回复
热议问题