Asp.net mvc 突出显示当前选定的菜单(基于操作和控制器和路由值)

Asp.net mvc 突出显示当前选定的菜单(基于操作和控制器和路由值),asp.net-mvc,Asp.net Mvc,我正在尝试为我的MVC3解决方案实现一个菜单。在此菜单中,我有两种链接: 基于action+controller的经典链接 示例1:action=“Studies”+controller=“Main” 示例2:action=“Contact”+controller=“Main” 基于action+controller+RouteValue的更复杂的链接 示例3:action=“List”+controller=“Project”+路由值= 新建{category=“BANK”} 示例4:

我正在尝试为我的MVC3解决方案实现一个菜单。在此菜单中,我有两种链接:

  • 基于action+controller的经典链接
示例1:action=“Studies”+controller=“Main”

示例2:action=“Contact”+controller=“Main”

  • 基于action+controller+RouteValue的更复杂的链接
示例3:action=“List”+controller=“Project”+路由值= 新建{category=“BANK”}

示例4:action=“List”+controller=“Project”+路由值= 新{category=“PHARMA”}

菜单如下所示:

public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
    {
        var route = htmlHelper.ViewContext.RequestContext.RouteData;
        var controller = route.GetRequiredString("controller");
        var action = route.GetRequiredString("action");
    
        // some code here...

        if ((controller == controllerName) && (action == actionName))
        {
            tag.AddCssClass("active");
        }
        else
        {
            tag.AddCssClass("inactive");
        }

        // some code here...
    }
  • 研究
  • 接触
  • 银行
  • 制药公司
我想根据活动页面选择当前活动的菜单项。为了实现这一点,我实现了如下htmlHelper:

public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
    {
        var route = htmlHelper.ViewContext.RequestContext.RouteData;
        var controller = route.GetRequiredString("controller");
        var action = route.GetRequiredString("action");
    
        // some code here...

        if ((controller == controllerName) && (action == actionName))
        {
            tag.AddCssClass("active");
        }
        else
        {
            tag.AddCssClass("inactive");
        }

        // some code here...
    }
此基本实现的问题在于,激活/禁用菜单项的条件仅基于动作和控制器值。我还需要检查“复杂链接”的RouteValue(示例3和4)

我如何实现这一点

谢谢你的帮助

public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
    var route = htmlHelper.ViewContext.RequestContext.RouteData;
    var rvd = HtmlHelper.AnonymousObjectToHtmlAttributes(routeValues);

    // some code here...

    if (IsRouteValuesMatch(rvd, route))
    {
        tag.AddCssClass("active");
    }
    else
    {
        tag.AddCssClass("inactive");
    }

    // some code here...
}

private static bool IsRouteValuesMatch(RouteValueDictionary rvd, RouteData routeData)
{
    foreach (var item in rvd)
    {
        var value1 = item.Value as string;
        var value2 = routeData.Values[item.Key] as string;
        if (!string.Equals(value1, value2, StringComparison.OrdinalIgnoreCase))
        {
            return false;
        }
    }
    return true;
}