<p>I need to determine if I'm on a particular view.  My use case is that I'd like to decorate navigation elements with an "on" class for the current view.  Is there a built in way of doing this?</p>

[{'answer_id': 40538, 'author': 'Jim Geurts', 'author_id': 3085, 'author_profile': 'https://Stackoverflow.com/users/3085', 'pm_score': 2, 'selected': False, 'text': '<p>My current solution is with extension methods:</p>\n\n<pre><code>public static class UrlHelperExtensions\n{\n    /// &lt;summary&gt;\n    /// Determines if the current view equals the specified action\n    /// &lt;/summary&gt;\n    /// &lt;typeparam name="TController"&gt;The type of the controller.&lt;/typeparam&gt;\n    /// &lt;param name="helper"&gt;Url Helper&lt;/param&gt;\n    /// &lt;param name="action"&gt;The action to check.&lt;/param&gt;\n    /// &lt;returns&gt;\n    ///     &lt;c&gt;true&lt;/c&gt; if the specified action is the current view; otherwise, &lt;c&gt;false&lt;/c&gt;.\n    /// &lt;/returns&gt;\n    public static bool IsAction&lt;TController&gt;(this UrlHelper helper, LambdaExpression action) where TController : Controller\n    {\n        MethodCallExpression call = action.Body as MethodCallExpression;\n        if (call == null)\n        {\n            throw new ArgumentException("Expression must be a method call", "action");\n        }\n\n        return (call.Method.Name.Equals(helper.ViewContext.ViewName, StringComparison.OrdinalIgnoreCase) &amp;&amp;\n                typeof(TController) == helper.ViewContext.Controller.GetType());\n    }\n\n    /// &lt;summary&gt;\n    /// Determines if the current view equals the specified action\n    /// &lt;/summary&gt;\n    /// &lt;param name="helper"&gt;Url Helper&lt;/param&gt;\n    /// &lt;param name="actionName"&gt;Name of the action.&lt;/param&gt;\n    /// &lt;returns&gt;\n    ///     &lt;c&gt;true&lt;/c&gt; if the specified action is the current view; otherwise, &lt;c&gt;false&lt;/c&gt;.\n    /// &lt;/returns&gt;\n    public static bool IsAction(this UrlHelper helper, string actionName)\n    {\n        if (String.IsNullOrEmpty(actionName))\n        {\n            throw new ArgumentException("Please specify the name of the action", "actionName");\n        }\n        string controllerName = helper.ViewContext.RouteData.GetRequiredString("controller");\n        return IsAction(helper, actionName, controllerName);\n    }\n\n    /// &lt;summary&gt;\n    /// Determines if the current view equals the specified action\n    /// &lt;/summary&gt;\n    /// &lt;param name="helper"&gt;Url Helper&lt;/param&gt;\n    /// &lt;param name="actionName"&gt;Name of the action.&lt;/param&gt;\n    /// &lt;param name="controllerName"&gt;Name of the controller.&lt;/param&gt;\n    /// &lt;returns&gt;\n    ///     &lt;c&gt;true&lt;/c&gt; if the specified action is the current view; otherwise, &lt;c&gt;false&lt;/c&gt;.\n    /// &lt;/returns&gt;\n    public static bool IsAction(this UrlHelper helper, string actionName, string controllerName)\n    {\n        if (String.IsNullOrEmpty(actionName))\n        {\n            throw new ArgumentException("Please specify the name of the action", "actionName");\n        }\n        if (String.IsNullOrEmpty(controllerName))\n        {\n            throw new ArgumentException("Please specify the name of the controller", "controllerName");\n        }\n\n        if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))\n        {\n            controllerName = controllerName + "Controller";\n        }\n\n        bool isOnView = helper.ViewContext.ViewName.SafeEquals(actionName, StringComparison.OrdinalIgnoreCase);\n        return isOnView &amp;&amp; helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase);\n    }\n}\n</code></pre>\n'}, {'answer_id': 45103, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 1, 'selected': False, 'text': '<p>Here is something a little different, use a FilterAttribute:</p>\n\n<pre><code>    [NavigationLocationFilter("Products")]\n    public ViewResult List()\n    {\n        return View();\n    }\n</code></pre>\n\n<p>...</p>\n\n<pre><code>public class NavigationLocationFilterAttribute : ActionFilterAttribute\n{\n    public string CurrentLocation { get; set; }\n\n    public NavigationLocationFilterAttribute(string currentLocation)\n    {\n        CurrentLocation = currentLocation;\n    }\n\n    public override void OnActionExecuting(ActionExecutingContext filterContext)\n    {\n        var controller = (Controller)filterContext.Controller;\n        controller.ViewData.Add("NavigationLocation", CurrentLocation);\n    }\n}\n</code></pre>\n\n<p>...</p>\n\n<p>And in the view:</p>\n\n<pre><code>&lt;%= ViewData["NavigationLocation"] %&gt;\n</code></pre>\n'}, {'answer_id': 2745475, 'author': 'memical', 'author_id': 322622, 'author_profile': 'https://Stackoverflow.com/users/322622', 'pm_score': 4, 'selected': True, 'text': '<p>Here what i am using. I think this is actually generated by the MVC project template in VS:</p>\n\n<pre><code>public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)\n    {\n        string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];\n        string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];\n\n        if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) &amp;&amp; currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))\n            return true;\n\n        return false;\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/40535', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3085/']