Asp.Net MVC:如何确定您当前是否在特定视图上

Jim*_*rts 9 c# asp.net-mvc

我需要确定我是否在某个特定视图上.我的用例是我想用当前视图的"on"类来装饰导航元素.这样做有内置的方法吗?

mem*_*cal 7

在这里我正在使用.我认为这实际上是由VS中的MVC项目模板生成的:

public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
    {
        string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
        string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

        if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
            return true;

        return false;
    }
Run Code Online (Sandbox Code Playgroud)