Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
    }
}
like image 562
griegs Avatar asked May 27 '10 05:05

griegs


1 Answers

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"];
}
like image 97
Darin Dimitrov Avatar answered Oct 17 '22 03:10

Darin Dimitrov