How to get the Current Controller Name, Action, or ID in ASP.NET MVC

«««««««««««««««  Updated!!! A commenter asked for the area name, so I’ve included that in the code as well.
«««««««««««««««

Luckily I’ve been back in ASP.NET MVC lately! On a new project I needed to check the current controller and actions for highlighting the current item in the menu. SO I dug up some of my custom code that seems to still apply to the latest ASP.NET MVC.

Usage

From your view you can simply use the extension methods off the Html object:

@Html.Controller();
@Html.Action();
@Html.Id();
@Html.Area();

Show me the Code!

Here it is:

public static class HtmlRequestHelper
{
    public static string Id(this HtmlHelper htmlHelper)
    {
        var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
        
        if (routeValues.ContainsKey("id"))
            return (string)routeValues["id"];
        else if (HttpContext.Current.Request.QueryString.AllKeys.Contains("id"))
            return HttpContext.Current.Request.QueryString["id"];
        
        return string.Empty;
    }
    
    public static string Controller(this HtmlHelper htmlHelper)
    {
        var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
        
        if (routeValues.ContainsKey("controller"))
            return (string)routeValues["controller"];
        
        return string.Empty;
    }
    
    public static string Action(this HtmlHelper htmlHelper)
    {
        var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;
        
        if (routeValues.ContainsKey("action"))
            return (string)routeValues["action"];
        
        return string.Empty;
    }
    
    public static string Area(this HtmlHelper htmlHelper)
    {
        var dataTokens = HttpContext.Current.Request.RequestContext.RouteData.DataTokens;
        
        if (dataTokens.ContainsKey("area"))
            return (string)dataTokens["area"];
        
        return string.Empty;
    }
}

Just add this class into your project. You may need to reference the namespace in the view folder web.config. Let me know if I need to add that part in.

Thanks!

Published: October 03, 2014

blog comments powered by Disqus