Which action is executing during an HtmlHelper extension method

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?

Answers


Try this

var action = HtmlHelper.ViewContext.RouteData.Values["action"];
var controller = HtmlHelper.ViewContext.RouteData.Values["controller"];

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.


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.


Need Your Help

uninitialized constant ActiveRecord::Base::StrictgitSanitizer

ruby ruby-on-rails-3 rake

I am trying to run rake for testing my Rails 3.2.3 app and I'm getting this weird error

Java: Lazy loading Singleton and reflection attack?

java reflection singleton lazy-loading

If I implement a Singleton either through holder idiom or double checked locking, but instead of calling 'getInstance()', use reflection to instantiate it, and then call 'getInstance()' on it, this...