Export (0) Print
Expand All

How to: Configure Multiple Site Maps and Site-Map Providers

By default, ASP.NET site navigation works with an XML file that is named Web.sitemap that describes the hierarchy of the Web site. However, you might want to use more than one site-map file or site-map provider to describe the navigation structure of a complete Web site.

For example, you could have a main site map that references a child site map. The main site map could be the following:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode  title="Home Page"  description="">
    <siteMapNode title="First Level 1"  description="" />
    <siteMapNode  title="First Level 2" description="">
      <siteMapNode  title="Second Level 1" description=""/>
      <siteMapNode  title="Second Level 2" description=""/>
    </siteMapNode>
    <siteMapNode siteMapFile="child.sitemap" />
  </siteMapNode>
</siteMap>

The child map could be the following:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode  title="Child First Level 1"  description="">
    <siteMapNode title="Child Second Level 1"  description="" />
    <siteMapNode  title="Child Second Level 2"  description="" />
    <siteMapNode  title=""  description="" />
  </siteMapNode>
</siteMap>

The resulting menu from these two site maps will appear as follows:

Home Page
  First Level 1
  First Level 2
    Second Level 1
    Second Level 2
  Child First Level 1
    Child Second Level 1
    Child Second Level 2

To configure multiple site maps for a single site, you start with a site map in the root of the application. Configure the root provider as the default site-map provider in the Web.config file. Then link to child site maps or providers by referencing them in a SiteMapNode object.

To link to a child site-map file from a parent site map

  • From the parent site map, create a SiteMapNode in the location in the navigation structure where you want the child site map to be displayed.

    For example, if you are using the default XmlSiteMapProvider class, add the following SiteMapNode in the appropriate location in the Web.sitemap file.

    <siteMapNode siteMapFile="MySiteMap.sitemap" />
    

    The siteMapFile attribute can take one of the following forms:

    • An application-relative reference, such as ~/MySiteMap.sitemap.

    • A virtual path, such as /Customers/MySiteMap.sitemap.

    • A path reference that is relative to the location of the current site-map file, such as Guests/MySiteMap.sitemap.

      NoteNote

      Do not provide a Url, title, or description attribute for the siteMapNode element when specifying a siteMapFile attribute.

    For more information about how to create a site-map file, see ASP.NET Site Maps.

To link to a custom site-map provider from a parent site map

  1. From the parent site map, create a SiteMapNode in the location in the navigation structure where you want the child site map to be displayed.

    For example, if you are using the default XmlSiteMapProvider class, open the Web.sitemap file and add the following SiteMapNode in the appropriate location in the hierarchy:

    <siteMapNode provider="SimpleTextSiteMapProvider" />
    
    NoteNote

    The provider attribute corresponds to the provider's name attribute in the Web.config file.

  2. Add the custom site-map provider to the Web.config file by using an add element.

    The following code adds the custom provider named SimpleTextSiteMapProvider, but maintains XmlSiteMapProvider as the default site-map provider.

    <configuration>
      <!-- other configuration sections -->
      <system.web>
        <!-- other configuration sections -->
        <siteMap defaultProvider="XmlSiteMapProvider">
          <providers>
            <add
              name="SimpleTextSiteMapProvider"
          type="Samples.AspNet.SimpleTextSiteMapProvider,Samples.AspNet"
              siteMapFile = "siteMap.txt" />
          </providers>
        </siteMap>
      </system.web>
    </configuration>
    

    For more information about creating a custom site-map provider, see Implementing ASP.NET Site-Map Providers.

Linking site maps together as in the previous examples enables you to generate a single site-map structure from many pieces. Alternatively, you can add references to different site maps in the Web.config file, which makes them appear like different providers. This is useful when different areas of a Web site need different navigational structures.

To configure multiple site maps in the Web.config file

  • In the Web.config file, locate the siteMap section and create an add element for each site map.

    The following example shows how to add two site maps.

    <configuration>
      <!-- other configuration sections -->
      <system.web>
        <!-- other configuration sections -->
        <siteMap defaultProvider="XmlSiteMapProvider">
         <providers>
           <add 
             name="Company1SiteMap"
             type="System.Web.XmlSiteMapProvider" 
             siteMapFile="~/Company1/Company1.sitemap" />
           <add 
             name="Company2SiteMap"
             type="System.Web.XmlSiteMapProvider" 
             siteMapFile="~/Company2/Company2.sitemap" />
         </providers>
        </siteMap>
      </system.web>
    </configuration>
    

    With this configuration, you can use the ~/Company1/Company1.sitemap and ~/Company2/Company2.sitemap files with the navigation API members and with navigation controls, such as SiteMapPath, TreeView, and Menu. To do so, you set the relevant SiteMapProvider property to Company1SiteMap or Company2SiteMap.

    For more information about adding navigation controls to a Web page, see How to: Add Simple Site Navigation.

Community Additions

ADD
Show:
© 2014 Microsoft