Export (0) Print
Expand All

SiteMap Class

The SiteMap class is an in-memory representation of the navigation structure for a site, which is provided by one or more site map providers. This class cannot be inherited.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

public static class SiteMap
public final class SiteMap
public final class SiteMap
Not applicable.

Site maps are used in conjunction with the SiteMapDataSource, SiteMapPath, and other controls to render an effective navigation interface for users to navigate a Web site. A SiteMap object is a component of the ASP.NET site navigation infrastructure that provides access to read-only site map information for page and control developers using navigation and SiteMapDataSource controls. Other components of the ASP.NET site navigation infrastructure include the SiteMapProvider and XmlSiteMapProvider classes, the Web.sitemap file, and the various controls that work with the SiteMap class, such as the SiteMapPath control, to render content and menus.

The SiteMap has several functions:

  • It provides the root node of the site navigation hierarchy (there can be only one root node).

  • It identifies which site map provider is the principal, or default, provider.

  • It keeps track of all the provider objects that are used to create the SiteMap.

Fundamentally, the SiteMap is a container for a hierarchical collection of SiteMapNode objects. However, the SiteMap does not maintain the relationships between the nodes; rather, it delegates this to the site map providers. The SiteMap acts as an interface to the site navigation information (in the form of SiteMapNode objects) that is held by these site map providers. It keeps track of the providers from which it can access site navigation data and exposes its Providers collection. A provider can store its site map information in any way—as long as it extends the SiteMapProvider abstract class, the SiteMap class can be initialized successfully from it.

The default provider for the SiteMap class is the XmlSiteMapProvider class, which works with an XML configuration file, Web.sitemap. However, you can write your own SiteMapProvider object, and use it to initialize the site map for your site. For an example of a custom site map provider, see SiteMapProvider.

A default site map configuration is specified that manages the site navigation configuration. You can override this configuration in the Web.config file, should you choose to write your own site map provider.

The SiteMap class offers only static members for performance and it can be accessed at any time in a given site from a Web Form or Web server control.

TopicLocation
How to: Programmatically Enumerate Site-Map NodesBuilding ASP .NET Web Applications
How to: Configure Multiple Site Maps and Site-Map ProvidersBuilding ASP .NET Web Applications
How to: Filter the Nodes Retrieved by SiteMapDataSource Web Server ControlsBuilding ASP .NET Web Applications
How to: Display Site-Map Data in Non-Hierarchical Web Server ControlsBuilding ASP .NET Web Applications
How to: Add Simple Site NavigationBuilding ASP .NET Web Applications
How to: Implement ASP.NET Site-Map ProvidersBuilding ASP .NET Web Applications
How to: Programmatically Enumerate Site-Map NodesBuilding ASP .NET Web Applications
How to: Configure Multiple Site Maps and Site-Map ProvidersBuilding ASP .NET Web Applications
How to: Filter the Nodes Retrieved by SiteMapDataSource Web Server ControlsBuilding ASP .NET Web Applications
How to: Display Site-Map Data in Non-Hierarchical Web Server ControlsBuilding ASP .NET Web Applications
How to: Add Simple Site NavigationBuilding ASP .NET Web Applications
How to: Implement ASP.NET Site-Map ProvidersBuilding ASP .NET Web Applications
How to: Add Simple Site NavigationBuilding ASP .NET Web Applications in Visual Studio

This section contains two code examples. The first code example demonstrates how to declare a site map provider in a Web.config file. The second code example demonstrates how to use a SiteMap object.

The following code example is a Web.config configuration section that demonstrates how an XmlSiteMapProvider object is declared as the default SiteMapProvider object.

<siteMap defaultProvider="MyXmlSiteMapProvider" enabled="true">
  <providers>
    <add name="MyXmlSiteMapProvider"
      description="The site map provider that reads in the .sitemap XML files."
      type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
      siteMapFile="my.sitemap" />
  </providers>
</siteMap>

The following code example demonstrates how to use the SiteMap object by accessing its static members to display information about the RootNode and CurrentNode properties. Compare it to the output that is rendered by the SiteMapPath control.

<%@ Page language="c#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
    // Examine the CurrentNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.CurrentNode.Title + "<br />");
    Response.Write("<font COLOR='red'>" + SiteMap.CurrentNode.Url + "</font><br />");

    // What nodes are children of the CurrentNode?
    if (SiteMap.CurrentNode.HasChildNodes) {
        IEnumerator childNodesEnumerator = SiteMap.CurrentNode.ChildNodes.GetEnumerator();
        while (childNodesEnumerator.MoveNext()) {
            // Prints the Title of each node.
            Response.Write(childNodesEnumerator.Current.ToString() + "<br />");
        }
    }
    Response.Write("<hr />");

    // Examine the RootNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.RootNode.Title + "<br />");
    Response.Write(SiteMap.RootNode.Url + "<br />");

    // What nodes are children of the RootNode?
    if (SiteMap.RootNode.HasChildNodes) {
        IEnumerator rootNodesChildrenEnumerator = SiteMap.RootNode.ChildNodes.GetEnumerator();
        while (rootNodesChildrenEnumerator.MoveNext()) {
            // Prints the Title of each node.
            Response.Write(rootNodesChildrenEnumerator.Current.ToString() + "<br />");
        }
    }

}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" runat="server">
      <asp:SiteMapPath
          runat="server"
        ID="SiteMapPath1"
        ShowToolTips="false"/>
    </form>
  </body>
</html>

System.Object
  System.Web.SiteMap

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft