Asp.Net MVC: How to determine if you're currently on a specific view

前端 未结 3 1764
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 01:22

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

3条回答
  •  一向
    一向 (楼主)
    2021-02-06 02:17

    Here what i am using. I think this is actually generated by the MVC project template in VS:

    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;
        }
    

提交回复
热议问题