这个功能不是内置的(目前),但是有一种方法可以实现它,方法是构建您自己的可见性提供程序并使用 SourceMetaData 将菜单名称传递给可见性逻辑。
/// <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
}
然后,您可以通过给它们一个“名称”SourceMetadata 属性来命名每个菜单。
@Html.MvcSiteMap().Menu(new { name = "MainMenu" })
@Html.MvcSiteMap().Menu(new { name = "PageMenu" })
然后在您的配置中使用 CustomFilteredSiteMapVisibilityProvider 而不是 FilteredVisibilityProvider。有关完整示例,请参见此答案。