I'm working on a menu-generating HtmlHelper extension method. This method will need to know which Action is being executed. So if Home/Index is executing, the extension method would show all links to other actions that're "coordinated." In a sense, all I need to know during the execution of the Home controller's Index action is the name of the Controller and the name of the Action that are being executed so that other logic can be executed. Is this possible?
+2
A:
Try this
var action = HtmlHelper.ViewContext.RouteData.Values["action"];
var controller = HtmlHelper.ViewContext.RouteData.Values["controller"];
Nick Berardi
2008-12-01 19:56:01
Sir, you are a god among men. Thank you SO very much.
brady gaster
2008-12-01 20:20:32
A:
I do something similar with a filter attribute. You can get the action name like this:
filterContext.RouteData.Values["action"].ToString();
I use this to disable the menu item that represents the current context.
Tim Scott
2008-12-01 20:24:11
A:
I need something like this, but not quite. I would love to be able to get some strongly typed way of knowing which action is executing.
So clarify in doing AOP where i only allow access to a given action if the user has rights for that action.
The problem with using a string for determining which rule to check for, is that if some developer renames an action, I wont get a compile error telling me that my rule is broken.
Christian Nielsen
2009-02-19 13:05:07