选项1
一种方法是明确设置起始节点。首先,给起始节点一个显式的键:
<mvcSiteMapNode title="Help" controller="Help" action="Index" key="HelpKey">
<mvcSiteMapNode title="T1" controller="Help" action="T1" />
<mvcSiteMapNode title="T2" controller="Help" action="T2" />
<mvcSiteMapNode title="T3" controller="Help" action="T3" />
<mvcSiteMapNode title="T4" controller="Help" action="T4" />
<mvcSiteMapNode title="T5" controller="Help" action="T5" />
<mvcSiteMapNode title="T6" controller="Help" action="T6" />
</mvcSiteMapNode>
并使用菜单重载:
@Html.MvcSiteMap().Menu(Html.MvcSiteMap().SiteMap.FindSiteMapNodeFromKey("HelpKey"), false, false)
但这样做并没有给你太多的控制权。
选项 2
您还可以使用自定义可见性提供程序。您可以使用 sourceMetaData 字典将自定义数据传递给可见性提供程序,因此您可以为每个菜单实例创建一个单独的“名称”:
@Html.MvcSiteMap().Menu(new { name = "MainMenu" })
@Html.MvcSiteMap().Menu(new { name = "HelpMenu" })
然后,您的自定义可见性提供程序可以使用 FilteredSiteMapNodeVisibilityProvider 用于您的自定义“名称”属性的相同逻辑:
/// <summary>
/// Filtered SiteMapNode Visibility Provider for use with named controls.
///
/// Rules are parsed left-to-right, first match wins. Asterisk can be used to match any control or any control name. Exclamation mark can be used to negate a match.
/// </summary>
public class CustomFilteredSiteMapNodeVisibilityProvider
: SiteMapNodeVisibilityProviderBase
{
#region ISiteMapNodeVisibilityProvider Members
/// <summary>
/// Determines whether the node is visible.
/// </summary>
/// <param name="node">The node.</param>
/// <param name="sourceMetadata">The source metadata.</param>
/// <returns>
/// <c>true</c> if the specified node is visible; otherwise, <c>false</c>.
/// </returns>
public override bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata)
{
// Is a visibility attribute specified?
string visibility = string.Empty;
if (node.Attributes.ContainsKey("visibility"))
{
visibility = node.Attributes["visibility"].GetType().Equals(typeof(string)) ? node.Attributes["visibility"].ToString() : string.Empty;
}
if (string.IsNullOrEmpty(visibility))
{
return true;
}
visibility = visibility.Trim();
// Check for the source HtmlHelper
if (sourceMetadata["HtmlHelper"] == null)
{
return true;
}
string htmlHelper = sourceMetadata["HtmlHelper"].ToString();
htmlHelper = htmlHelper.Substring(htmlHelper.LastIndexOf(".") + 1);
string name = sourceMetadata["name"].ToString();
// All set. Now parse the visibility variable.
foreach (string visibilityKeyword in visibility.Split(new[] { ',', ';' }))
{
if (visibilityKeyword == htmlHelper || visibilityKeyword == name || visibilityKeyword == "*")
{
return true;
}
else if (visibilityKeyword == "!" + htmlHelper || visibilityKeyword == "!" + name || visibilityKeyword == "!*")
{
return false;
}
}
// Still nothing? Then it's OK!
return true;
}
#endregion
}
然后更新您的配置以使用名称而不是控件类型。
<mvcSiteMapNode title="Home" controller="Home" action="Index" changeFrequency="Always" >
<mvcSiteMapNode title="Home" controller="Home" action="Index" visibility="MainMenu,!*">
<mvcSiteMapNode title="T1" controller="Home" action="T1" />
<mvcSiteMapNode title="T2" controller="Home" action="T2"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Help" controller="Help" action="Index" visibility="HelpMenu,!*">
<mvcSiteMapNode title="T1" controller="Help" action="T1" />
<mvcSiteMapNode title="T2" controller="Help" action="T2" />
<mvcSiteMapNode title="T3" controller="Help" action="T3" />
<mvcSiteMapNode title="T4" controller="Help" action="T4" />
<mvcSiteMapNode title="T5" controller="Help" action="T5" />
<mvcSiteMapNode title="T6" controller="Help" action="T6" />
</mvcSiteMapNode>
</mvcSiteMapNode>
并且不要忘记将您的自定义可见性提供程序设置为配置中的默认值。
使用内部 DI (web.config):
<appSettings>
<add key="MvcSiteMapProvider_DefaultSiteMapNodeVisibiltyProvider" value="MyNamespace.CustomFilteredSiteMapNodeVisibilityProvider, MyAssemblyName"/>
</appSettings>
使用外部 DI(在 DI 模块中 - 显示了 Ninject 示例):
this.Kernel.Bind<ISiteMapNodeVisibilityProviderStrategy>().To<SiteMapNodeVisibilityProviderStrategy>()
.WithConstructorArgument("defaultProviderName", "MyNamespace.CustomFilteredSiteMapNodeVisibilityProvider, MyAssemblyName");
注意:如果您需要关闭父节点的可见性但保持子节点可见,则应使用 Menu()、SiteMap() 或 SiteMapPath() 的重载之一,这些重载允许您将 visibilityAffectsDescendants 参数设置为 false。默认情况下是真的。