How do I get the action name from a base controller?

霸气de小男生 提交于 2019-12-19 08:05:08

问题


I'd like to implement a base controller on one of my controllers. Within that base controller, I'd like to be able to get the current executing ActionResult name.

How would I go about doing this?

public class HomeController : ControllerBase
{
    public ActionResult Index()
    {

And;

public class ControllerBase : Controller
{
    public ControllerBase()
    {
        //method which will get the executing ActionResult
    }
}

回答1:


You can't know this in the constructor of the controller as the controller is currently being instantiated and no action could be called yet. However you could override the Initialize method and fetch the action name from the routing engine:

protected override void Initialize(RequestContext requestContext)
{
    base.Initialize(requestContext);
    var actionName = requestContext.RouteData.Values["action"];
}


来源:https://stackoverflow.com/questions/2918671/how-do-i-get-the-action-name-from-a-base-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!