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

Posted

技术标签:

【中文标题】Asp.Net MVC:如何确定您当前是不是在特定视图上【英文标题】:Asp.Net MVC: How to determine if you're currently on a specific viewAsp.Net MVC:如何确定您当前是否在特定视图上 【发布时间】:2010-09-07 14:52:16 【问题描述】:

我需要确定我是否处于特定视图中。我的用例是我想用当前视图的“on”类来装饰导航元素。有内置的方法吗?

【问题讨论】:

【参考方案1】:

这是我正在使用的。我认为这实际上是由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;
    

【讨论】:

【参考方案2】:

我目前的解决方案是使用扩展方法:

public static class UrlHelperExtensions

    /// <summary>
    /// Determines if the current view equals the specified action
    /// </summary>
    /// <typeparam name="TController">The type of the controller.</typeparam>
    /// <param name="helper">Url Helper</param>
    /// <param name="action">The action to check.</param>
    /// <returns>
    ///     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsAction<TController>(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());
    

    /// <summary>
    /// Determines if the current view equals the specified action
    /// </summary>
    /// <param name="helper">Url Helper</param>
    /// <param name="actionName">Name of the action.</param>
    /// <returns>
    ///     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    /// </returns>
    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);
    

    /// <summary>
    /// Determines if the current view equals the specified action
    /// </summary>
    /// <param name="helper">Url Helper</param>
    /// <param name="actionName">Name of the action.</param>
    /// <param name="controllerName">Name of the controller.</param>
    /// <returns>
    ///     <c>true</c> if the specified action is the current view; otherwise, <c>false</c>.
    /// </returns>
    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);
    

【讨论】:

【参考方案3】:

这里有点不同,使用 FilterAttribute:

    [NavigationLocationFilter("Products")]
    public ViewResult List()
    
        return View();
    

...

public class NavigationLocationFilterAttribute : ActionFilterAttribute

    public string CurrentLocation  get; set; 

    public NavigationLocationFilterAttribute(string currentLocation)
    
        CurrentLocation = currentLocation;
    

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    
        var controller = (Controller)filterContext.Controller;
        controller.ViewData.Add("NavigationLocation", CurrentLocation);
    

...

在视图中:

<%= ViewData["NavigationLocation"] %>

【讨论】:

以上是关于Asp.Net MVC:如何确定您当前是不是在特定视图上的主要内容,如果未能解决你的问题,请参考以下文章

如何从 HttpContext 获取 ASP.NET Core MVC 过滤器

如何确定当前在 ASP.Net 中是不是有任何文件正在上传?

Asp.Net Mvc 突出显示当前页面链接技术?

ASP.NET MVC 中返回视图的 URL

如何在 ASP.NET MVC 中控制局部视图的条件显示

如何在 ASP.NET MVC 5.2.3 和 Katana 中正确设置双重身份验证?