How to get controller and action name in OnActionExecuting?

asp.netasp.net Mvc-4

asp.net Problem Overview


Is it possible to figure out the currently executing controller/action in OnActionExecuting?

asp.net Solutions


Solution 1 - asp.net

You could try the ActionDescriptor of the ActionExecutingContext as follows:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
   string actionName = filterContext.ActionDescriptor.ActionName;
   string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName
   .....
   base.OnActionExecuting(filterContext);
}

Solution 2 - asp.net

You can use ActionDescriptor of ActionExecutingContext

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var descriptor = filterContext.ActionDescriptor;
    var actionName = descriptor.ActionName;
    var controllerName = descriptor.ControllerDescriptor.ControllerName;
    ......
    base.OnActionExecuting(filterContext);
}

Solution 3 - asp.net

You could look at the RouteData:

    Request.RequestContext.RouteData.Values["Controller"];
    Request.RequestContext.RouteData.Values["Action"];

Solution 4 - asp.net

For ASP.NET Core, Use the following,

var controllerName = ((ControllerBase)filterContext.Controller)
   .ControllerContext.ActionDescriptor.ControllerName;
var actionName = ((ControllerBase)filterContext.Controller)
   .ControllerContext.ActionDescriptor.ActionName;

Solution 5 - asp.net

ActionExecutingContext context this is going to be your context....

var actionName = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ActionName;
var controllerName = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ControllerName;

Solution 6 - asp.net

var controllerName = (string)routingValues["controller"];

var actionName = (string)routingValues["action"];

Solution 7 - asp.net

I guess you use Attributes (e.g. ActionFilterAttribute), if it so, you can get

  • action name form attribute constructor:

        public CustomProfileAttribute([CallerMemberName]  string caller = null)
        {
            _actionName = caller;
        }
    
  • controller name:

        public CustomProfileAttribute([CallerMemberName]  string caller = null)
        {
            _controllerName = context.Controller.ToString()
    
  • or both controller and action:

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var controllerAndAction = context.ActionDescriptor.DisplayName;
    

    but in such case you will have som ugly name like controller.action (action)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionloyalflowView Question on Stackoverflow
Solution 1 - asp.netchridamView Answer on Stackoverflow
Solution 2 - asp.netSatpalView Answer on Stackoverflow
Solution 3 - asp.netOliverView Answer on Stackoverflow
Solution 4 - asp.netCircuit BreakerView Answer on Stackoverflow
Solution 5 - asp.netSowmiyaa SivakumarView Answer on Stackoverflow
Solution 6 - asp.netRohan RewaleView Answer on Stackoverflow
Solution 7 - asp.netMaxim KitsenkoView Answer on Stackoverflow