What action is performed during the HtmlHelper extension method

I am working on a menu-creating extension method HtmlHelper. This method should know what action is being performed. Therefore, if the Home / Index is executed, the extension method will show all links to other actions that are β€œcoordinated”. In a sense, all I need to know while the Home Index is executing is the name of the controller and the name of the action that is executed so that other logic can execute. Is it possible?

+4
source share
3 answers

try it

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

I am doing something similar with a filter attribute. You can get the action name as follows:

 filterContext.RouteData.Values["action"].ToString(); 

I use this to turn off a menu item that represents the current context.

0
source

I need something like that, but not really. I would like to be able to get a strongly typed way of knowing what action is being performed.

So, check in the AOP, where I only allow access to this action, if the user has rights to this action.

The problem with using a string to determine which rule to check is that if any developer renames the action, I will not get a compilation error telling me that my rule is broken.

0
source

All Articles