如何创建新模型?

时间:2013-12-10 14:11:06

标签: mvcsitemapprovider asp.net-mvc-sitemap

我希望我能够执行以下操作:@Html.MvcSiteMap().PageMenu()而不是@Html.MvcSiteMap().Menu("MenuBtnGroup")

主要是因为过滤器。

我希望某个链接只出现在页面或站点地图上,而不是出现在主应用程序菜单中

<mvcSiteMapNode title="Usuários" controller="Usuarios" action="Index">
    <mvcSiteMapNode title="Novo Usuário" action="Novo" visibility="SiteMapPathHelper,PAGEMENU-ONLY,!*" />
    <mvcSiteMapNode title="Detalhes" action="Detalhes" visibility="SiteMapPathHelper,!*" dynamicNodeProvider="UsuarioDynamicNodeProvider, Web">
        <mvcSiteMapNode title="Editar" action="Editar" inheritedRouteParameters="id" />
    </mvcSiteMapNode>
</mvcSiteMapNode>

1 个答案:

答案 0 :(得分:1)

此功能尚未内置(但),但有一种方法可以通过构建您自己的可见性提供程序并使用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。有关完整示例,请参阅this answer