I wanted to set a CSS class in my master page, which depends on the current controller and action. I can get to the current controller via ViewContext.Controller.GetType().Name
, but how do I get the current action (e.g. Index
, Show
etc.)?
11 Answers
In the RC you can also extract route data like the action method name like this
ViewContext.Controller.ValueProvider["action"].RawValue
ViewContext.Controller.ValueProvider["controller"].RawValue
ViewContext.Controller.ValueProvider["id"].RawValue
Update for MVC 3
ViewContext.Controller.ValueProvider.GetValue("action").RawValue
ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
ViewContext.Controller.ValueProvider.GetValue("id").RawValue
Update for MVC 4
ViewContext.Controller.RouteData.Values["action"]
ViewContext.Controller.RouteData.Values["controller"]
ViewContext.Controller.RouteData.Values["id"]
Update for MVC 4.5
ViewContext.RouteData.Values["action"]
ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["id"]

- 21,938
- 6
- 78
- 91

- 6,603
- 4
- 21
- 27
-
20I know this pre-dates V2, but it's now `ViewContext.Controller.ValueProvider.GetValue("action").RawValue` + variations – Chris S Oct 24 '10 at 15:03
-
7This syntax works in V4: (string)ViewContext.RouteData.Values["action"]; – kiprainey Feb 28 '13 at 14:23
-
2This solution does not work for me (MVC4 + .NET Framework 4.5.1). For me, it works the answer from Viacheslav Smityukh: ViewContext.RouteData.Values["action"], ViewContext.RouteData.Values["controller"], ViewContext.RouteData.Values["id"], – Tomas Kubes Feb 21 '14 at 11:55
-
@kiprainey In V3.5 too: http://msdn.microsoft.com/en-us/library/system.web.mvc.viewcontext_members(v=vs.90).aspx#memberList2 – Pluto Mar 12 '14 at 22:59
-
Do you know how I can get the Partial View name? I tried this and I always get Index which is a View but I want to get the partial views – user3281466 Oct 30 '14 at 12:56
-
2Try: (string)HttpContext.Request.RequestContext.RouteData.Values["action"]; – Martin Dawson Jun 18 '16 at 21:01
Use the ViewContext
and look at the RouteData
collection to extract both the controller and action elements. But I think setting some data variable that indicates the application context (e.g., "editmode" or "error") rather than controller/action reduces the coupling between your views and controllers.

- 18,024
- 7
- 45
- 70

- 524,688
- 99
- 697
- 795
-
you can get data in controller and pass it to view with DTO.http://stackoverflow.com/a/31749391/4293929 – Mustafa ASAN Jul 31 '15 at 16:04
-
5this must NOT be the accepted answer, see the most up voted answer instead. – Hakan Fıstık Apr 28 '16 at 05:53
-
2@HakamFostok I still think the advice to add semantic information to the model is a better method in all cases than to rely on route data (controller, action) in your view even if the other answer provides more detail on how to get that data. – tvanfosson Apr 28 '16 at 12:34
To get the current Id on a View:
ViewContext.RouteData.Values["id"].ToString()
To get the current controller:
ViewContext.RouteData.Values["controller"].ToString()

- 997
- 2
- 8
- 11
-
1ViewContext.RouteData.Values[
].ToString() throws an exception if the value does not exist in RouteData – Grizzly Peak Software Nov 24 '13 at 04:46 -
@ShaneLarson Just compare with null or check with `ViewContext.RouteData.Values.ContainsKey(
)` first. – Pluto Mar 12 '14 at 22:57
I know this is an older question, but I saw it and I thought you might be interested in an alternative version than letting your view handle retrieving the data it needs to do it's job.
An easier way in my opinion would be to override the OnActionExecuting method. You are passed the ActionExecutingContext that contains the ActionDescriptor member which you can use to obtain the information you are looking for, which is the ActionName and you can also reach the ControllerDescriptor and it contains the ControllerName.
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
ActionDescriptor actionDescriptor = filterContext.ActionDescriptor;
string actionName = actionDescriptor.ActionName;
string controllerName = actionDescriptor.ControllerDescriptor.ControllerName;
// Now that you have the values, set them somewhere and pass them down with your ViewModel
// This will keep your view cleaner and the controller will take care of everything that the view needs to do it's job.
}
Hope this helps. If anything, at least it will show an alternative for anybody else that comes by your question.

- 18,202
- 3
- 54
- 70
-
nice recommendation; helped me in implementing controller/action sensing intelligently. Thanks! – effkay Dec 14 '09 at 06:56
-
Getting the action desrciptor is what i needed, i couldn't find really any other solution so i just do it here and then shove what i want into the viewbag. – Chris Marisic Mar 22 '12 at 21:11
I saw different answers and came up with a class helper:
using System;
using System.Web.Mvc;
namespace MyMvcApp.Helpers {
public class LocationHelper {
public static bool IsCurrentControllerAndAction(string controllerName, string actionName, ViewContext viewContext) {
bool result = false;
string normalizedControllerName = controllerName.EndsWith("Controller") ? controllerName : String.Format("{0}Controller", controllerName);
if(viewContext == null) return false;
if(String.IsNullOrEmpty(actionName)) return false;
if (viewContext.Controller.GetType().Name.Equals(normalizedControllerName, StringComparison.InvariantCultureIgnoreCase) &&
viewContext.Controller.ValueProvider.GetValue("action").AttemptedValue.Equals(actionName, StringComparison.InvariantCultureIgnoreCase)) {
result = true;
}
return result;
}
}
}
So in View (or master/layout) you can use it like so (Razor syntax):
<div id="menucontainer">
<ul id="menu">
<li @if(MyMvcApp.Helpers.LocationHelper.IsCurrentControllerAndAction("home", "index", ViewContext)) {
@:class="selected"
}>@Html.ActionLink("Home", "Index", "Home")</li>
<li @if(MyMvcApp.Helpers.LocationHelper.IsCurrentControllerAndAction("account","logon", ViewContext)) {
@:class="selected"
}>@Html.ActionLink("Logon", "Logon", "Account")</li>
<li @if(MyMvcApp.Helpers.LocationHelper.IsCurrentControllerAndAction("home","about", ViewContext)) {
@:class="selected"
}>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</div>
Hope it helps.

- 2,137
- 1
- 17
- 15
You can get these data from RouteData of a ViewContext
ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["action"]

- 5,652
- 4
- 24
- 42
In MVC you should provide the View with all data, not let the View collect its own data so what you can do is to set the CSS class in your controller action.
ViewData["CssClass"] = "bold";
and pick out this value from your ViewData in your View

- 9,497
- 4
- 54
- 72
-
This would be my preferred method as well to keep the views from depending on the controller structure, but it can be done otherwise. – tvanfosson Dec 12 '08 at 11:52
-
1Not sure I would name it "CssClass" as that seems to indicate that display logic is creeping into your controller. – tvanfosson Dec 12 '08 at 11:55
-
Agreed, I usually do something like "Context", so it is not tied to presentation layer, and does not break if you rename the view either. – D'Arcy Rittich Dec 12 '08 at 13:02
I vote for this 2:
string currentActionName = ViewContext.RouteData.GetRequiredString("action");
and
string currentViewName = ((WebFormView)ViewContext.View).ViewPath;
You can retrive both physical name of current view and action that triggered it. It can be usefull in partial *.acmx pages to determine host container.
I am using ASP.NET MVC 4, and this what worked for me:
ControllerContext.Controller.ValueProvider.GetValue("controller").RawValue
ControllerContext.Controller.ValueProvider.GetValue("action").RawValue

- 15,852
- 13
- 78
- 101
Override this function in your controller
protected override void HandleUnknownAction(string actionName)
{ TempData["actionName"] = actionName;
View("urViewName").ExecuteResult(this.ControllerContext);
}

- 9,877
- 28
- 100
- 157

- 111
- 3
Extending Dale Ragan's answer, his example for reuse, create an ApplicationController class which derives from Controller, and in turn have all your other controllers derive from that ApplicationController class rather than Controller.
Example:
public class MyCustomApplicationController : Controller {}
public class HomeController : MyCustomApplicationController {}
On your new ApplicationController create a property named ExecutingAction with this signature:
protected ActionDescriptor ExecutingAction { get; set; }
And then in the OnActionExecuting method (from Dale Ragan's answer), simply assign the ActionDescriptor to this property and you can access it whenever you need it in any of your controllers.
string currentActionName = this.ExecutingAction.ActionName;

- 1
- 1

- 949
- 2
- 12
- 21