假设我有站点地图
<mvcSiteMapNode title="Home" controller="Home" action="Index" changeFrequency="Always" >
<mvcSiteMapNode title="Home" controller="Home" action="Index" visibility="MenuHelper,!*">
<mvcSiteMapNode title="T1" controller="Home" action="T1" />
<mvcSiteMapNode title="T2" controller="Home" action="T2"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Help" controller="Help" action="Index">
<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>
@Html.MvcSiteMap().Menu()
将显示上面包含节点的菜单
但是让我们说在页脚上我只想显示来自站点地图的特定节点,假设我想要显示节点Help
:
<ul>
<li>
<a href="/help">
</li>
<li>
<a href="/help/t1">T1</a>
</li>
<li>
<a href="/help/t2">T2</a>
</li>
//and so on....
</ul>
我该怎么做?
我猜我可能需要创建自定义模板,但如何将字符串值传递给自定义模板?
答案 0 :(得分:3)
执行此操作的一种方法是显式设置起始节点。首先,为起始节点提供一个明确的密钥:
<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)
但这样做并不能给你很大的控制权。
您还可以使用自定义可见性提供程序。您可以使用sourceMetaData字典将自定义数据传递给可见性提供程序,这样您就可以创建一个单独的名称&#34;对于每个菜单实例:
@Html.MvcSiteMap().Menu(new { name = "MainMenu" })
@Html.MvcSiteMap().Menu(new { name = "HelpMenu" })
然后,您的自定义可见性提供程序可以使用FilteredSiteMapNodeVisibilityProvider用于您的自定义&#34; name&#34;属性:
/// <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。这是默认的。