如何在OnActionExecuting中获取控制器和操作名称?

有没有可能找出OnActionExecuting当前正在执行的控制器/动作?

你可以尝试ActionExecutingContextActionDescriptor ,如下所示:

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

您可以使用ActionExecutingContext ActionDescriptor

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

你可以看看RouteData

  Request.RequestContext.RouteData.Values["Controller"]; Request.RequestContext.RouteData.Values["Action"]; 
 var controllerName = (string)routingValues["controller"]; var actionName = (string)routingValues["action"]; 

ActionExecutingContext上下文这将是你的上下文….

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