Getting current controller & action from within partial view

I'm using the following to get the current controller and action in MVC3:

var currentAction = routeData.GetRequiredString("action");
var currentController = routeData.GetRequiredString("controller");

This works perfectly. However, if I call this from within a partial view that is called from my layout, "Layout" is returned as the current controller. This is of course correct behaviour, but is there any way to access the parent controller's name?

Edit for further clarification:

I am calling my menu controller and partial view from within _Layout.cshtml:

@Html.Action("Menu", "Layout")

Then from within that Menu partial view, I am calling code which returns the current action and controller.


After your updated question and showing your code it is much more clear: you are not including a partial view. You are calling a child action. There's a huge difference between Html.Partial and Html.Action. So if you want to get the parent context inside this child action you could do this:

public ActionResult Menu()
    var rd = ControllerContext.ParentActionViewContext.RouteData;
    var currentAction = rd.GetRequiredString("action");
    var currentController = rd.GetRequiredString("controller");
    return View();

I stumbled on this page looking for a way to access the parent controllers name after a call using Partial


This can be done in the partial view as

    var controller = ViewContext.RouteData.GetRequiredString("controller");
    var action = ViewContext.RouteData.GetRequiredString("action");

Need Your Help

Setting the manufacturer in a VS 2008 Setup Project

visual-studio visual-studio-2008 installer installation windows-installer

I have a windows setup project that installs a service. All works well except for one thing: The default directory offered to the user during install is of the form "C:\Program Files\Microsoft\