How to determine controller from @Html.Action in MVC .Net
Given you have:
ControllerA
ControllerB
And in the view for ControllerA you invoke:
@Html.Action("ControllerB", "Home");
In the 'Home' action in ControllerB, how can you determine what the originally invoked action & controller were?
I know I can determine the URL from the http context, but for the life of me I cannot figure out how to use this to map back to the originally invoked controller and action.
Nb. The solution I'm looking for m开发者_JAVA百科ust be arbitrary depth. If ControllerB invokes Html.Action on ControllerC which invokes Html.Action on ControllerD, I need to be able to resolve that the original action was ControllerA::Home from ControllerD::Home.
Use the ControllerContext.IsChildAction
property to determine if you are in a child action, then the ControllerContext.ParentActionViewContext
property contains all the information you need. This has a RouteData
property which can give you the controller and action name.
精彩评论