Introduction
In order to get the current controller and action name in an ASP.NET MVC view, we use the RouteData object.
RouteData
The RouteData object contains information about the current routing path.
Get controller name
@ViewContext.RouteData.Values["controller"].ToString()
In preceding code, we use the ViewContext object to access the RouteData
of the current request. After that, we use the Values property to access the current routing path
values. By passing the controller
key to the Values property we can get the current
controller name.
Get action name
In the same way, we pass the action
key to the Values property to get the current
action name.
@ViewContext.RouteData.Values["action"].ToString()