Search code examples
asp.netasp.net-mvccustom-action-filter

Get Action name from URL inside ActionFilterAttribute


I am writing custom filter attribute inherited from ActionFilterAttribute class in ASP.Net MVC. I want to get name of action from url, I tried

ControllerContext controllerContext = new ControllerContext();
controllerContext.RouteData.Values["action"].ToString();

but there is no item in controllerContext.RouteData.Values, hence giving me null reference error. Is any other way to get the same?


Solution

  • Try the following

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
    
            string action = filterContext.ActionDescriptor.ActionName;
            string controller = filterContext.Controller.GetType().Name;
    
        }