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

前端 未结 3 1758
佛祖请我去吃肉
佛祖请我去吃肉 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 01:59

    My current solution is with extension methods:

    public static class UrlHelperExtensions
    {
        /// 
        /// Determines if the current view equals the specified action
        /// 
        /// The type of the controller.
        /// Url Helper
        /// The action to check.
        /// 
        ///     true if the specified action is the current view; otherwise, false.
        /// 
        public static bool IsAction(this UrlHelper helper, LambdaExpression action) where TController : Controller
        {
            MethodCallExpression call = action.Body as MethodCallExpression;
            if (call == null)
            {
                throw new ArgumentException("Expression must be a method call", "action");
            }
    
            return (call.Method.Name.Equals(helper.ViewContext.ViewName, StringComparison.OrdinalIgnoreCase) &&
                    typeof(TController) == helper.ViewContext.Controller.GetType());
        }
    
        /// 
        /// Determines if the current view equals the specified action
        /// 
        /// Url Helper
        /// Name of the action.
        /// 
        ///     true if the specified action is the current view; otherwise, false.
        /// 
        public static bool IsAction(this UrlHelper helper, string actionName)
        {
            if (String.IsNullOrEmpty(actionName))
            {
                throw new ArgumentException("Please specify the name of the action", "actionName");
            }
            string controllerName = helper.ViewContext.RouteData.GetRequiredString("controller");
            return IsAction(helper, actionName, controllerName);
        }
    
        /// 
        /// Determines if the current view equals the specified action
        /// 
        /// Url Helper
        /// Name of the action.
        /// Name of the controller.
        /// 
        ///     true if the specified action is the current view; otherwise, false.
        /// 
        public static bool IsAction(this UrlHelper helper, string actionName, string controllerName)
        {
            if (String.IsNullOrEmpty(actionName))
            {
                throw new ArgumentException("Please specify the name of the action", "actionName");
            }
            if (String.IsNullOrEmpty(controllerName))
            {
                throw new ArgumentException("Please specify the name of the controller", "controllerName");
            }
    
            if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
            {
                controllerName = controllerName + "Controller";
            }
    
            bool isOnView = helper.ViewContext.ViewName.SafeEquals(actionName, StringComparison.OrdinalIgnoreCase);
            return isOnView && helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase);
        }
    }
    

提交回复
热议问题