开发者

How to determine controller from @Html.Action in MVC .Net

开发者 https://www.devze.com 2023-03-23 13:23 出处:网络
Given you have: ControllerA ControllerB And in the view for ControllerA you invoke: @Html.Action(\"ControllerB\", \"Home\");

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号