如何仅使用一个SiteMap创建多个不同的布局?

时间:2013-03-04 04:50:26

标签: asp.net mvcsitemapprovider

假设我的页面上有3个区域有链接

  • 标题
  • 菜单
  • 页脚

每个都有不同的链接,但有些链接重叠:

Overview

我正在使用MVCSiteMapProvider来实现这一目标。我有一个SiteMap:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0"
    xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0 MvcSiteMapSchema.xsd"
    enableLocalization="false">

    <mvcSiteMapNode title="Home" controller="Home" action="Index">

        <mvcSiteMapNode title="Link 1" controller="" action="" visibility="Header, Footer" />
        <mvcSiteMapNode title="Link 2" controller="" action="" visibility="Menu" />
        <mvcSiteMapNode title="Link 3" controller="" action="" visibility="Header, Menu" />
        <mvcSiteMapNode title="Link 4" controller="" action="" visibility="Menu, Footer, Header" />

    </mvcSiteMapNode>

</mvcSiteMap>

我认为可能是Visibility是这样做的方式,但它并不像我想要的那样工作。

Public Class MenuVisibilityProvider
    Implements ISiteMapNodeVisibilityProvider

    Public Function IsVisible(ByVal node As SiteMapNode, ByVal context As HttpContext, ByVal sourceMetadata As IDictionary(Of String, Object)) As Boolean Implements ISiteMapNodeVisibilityProvider.IsVisible
        Dim visibility As String = node("visibility")

        If visibility IsNot Nothing Then Return True

        Select Case visibility
            Case "Menu"
            Case "Header"
            Case "Footer"
                Return True
        End Select

        Return False
    End Function
End Class

我最终得到了每个领域的所有链接。

编辑以澄清:

这是一个类似的问题,但也没有回答: https://stackoverflow.com/questions/12845929/how-to-show-partial-site-map-including-current-node-with-mvcsitemapprovider

也类似,但我不想制作多个SiteMaps:Using Multiple MvcSiteMaps

1 个答案:

答案 0 :(得分:2)

OP在这里。我只使用一个站点地图完成了这项工作。

要做到这一点:

  1. 我为每个sitemap元素添加了可见性标记,例如:

    <mvcSiteMapNode title="Login" controller="Members" action="Login" visibility="SideMenu Footer" />
    

    在此示例中,"SideMenu Footer"是我的代码。我稍后会使用String.Contains()来确定可见性。

  2. 我在Web.config中使用不同的siteMapNodeVisibilityProvider添加了多个不同的siteMap提供商:

    <siteMap defaultProvider="MvcSiteMapProvider" enabled="true">
        <providers>
            <clear />
            <add name="MvcSiteMapProvider" type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider" siteMapFile="~/Mvc.Sitemap" securityTrimmingEnabled="true" cacheDuration="5" enableLocalization="true" scanAssembliesForSiteMapNodes="true" includeAssembliesForScan="" excludeAssembliesForScan="" attributesToIgnore="visibility" nodeKeyGenerator="MvcSiteMapProvider.DefaultNodeKeyGenerator, MvcSiteMapProvider" controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver, MvcSiteMapProvider" actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver, MvcSiteMapProvider" aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider" siteMapNodeUrlResolver="MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider" siteMapNodeVisibilityProvider="MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider" siteMapProviderEventHandler="MvcSiteMapProvider.DefaultSiteMapProviderEventHandler, MvcSiteMapProvider" />
            <add name="NavSiteMapProvider"  ... siteMapNodeVisibilityProvider="RootNamespace.Namespace.NavVisibilityProvider, RootNamespace" ... />
            <add name="FooterSiteMapProvider"  ... siteMapNodeVisibilityProvider="RootNamespace.Namespace.FooterVisibilityProvider, RootNamespace" ... />
        </providers>
    </siteMap>
    
  3. 我创建了一个名为CustomVisibilityProvider的新代码文件(类)。在里面我为每个提供者创建了一个类(页脚,导航,菜单等)

  4.     ' Note: VB.NET :P
        Public Class MenuVisibilityProvider
            Implements ISiteMapNodeVisibilityProvider
    
            Public Function IsVisible(ByVal node As SiteMapNode, ByVal context As HttpContext, ByVal sourceMetadata As IDictionary(Of String, Object)) As Boolean Implements ISiteMapNodeVisibilityProvider.IsVisible
    
                Dim visibility As String = node("visibility")
    
                If visibility Is Nothing Then Return False
    
                If visibility.Contains("Menu") Then Return True
    
                Return False
            End Function
    
        End Class
    
        Public Class NavVisibilityProvider
            Implements ISiteMapNodeVisibilityProvider
    
            ...
                If visibility.Contains("Nav") Then Return True
            ...
        End Class
    
    1. 当您进入视图文件时:

      @Html.MvcSiteMap("FooterSiteMapProvider").Menu
      
    2. 注意:您也可以为菜单()提供自定义视图,以便导航,页脚,菜单等以不同方式呈现链接(某些可能是<ul></ul>格式,而其他可能是<a>标签)。