共用方式為


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

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

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

  • 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, add the following SiteMapNode in the appropriate location in the Web.sitemap file.

    <siteMapNode siteMapFile="MySiteMap.sitemap" />
    
    NoteNote

    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.

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

  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, open your 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. The custom site-map provider must be added to your Web.config file by using the add property. The following code adds the custom provider called SimpleTextSiteMapProvider, but maintains the 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.

Configuring Multiple Site Maps in the Web.config File

Linking site maps together as in the previous two examples allows 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, essentially making them appear like different providers. This is beneficial when different areas of your Web site need different navigational structures.

To configure multiple site maps in the Web.config file

  • In your Web.config file, locate the <siteMap> section. If it does not exist, use the following code. Otherwise, just insert the <add> sections.

    <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>
    

    Assuming that the ~/Company1/Company1.sitemap and ~/Company2/Company2.sitemap files exist, you can now use them with the navigation API members and with navigation controls, such as SiteMapPath, TreeView, and Menu by setting 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.

See Also

Tasks

How to: Add Simple Site Navigation

Concepts

ASP.NET Site Maps
ASP.NET Site Navigation Providers
ASP.NET Site-Map Security Trimming
Implementing ASP.NET Site-Map Providers
Securing ASP.NET Site Navigation
Securing Data Access

Other Resources

ASP.NET Application Security in Hosted Environments