Site Map in ASP.NET 3.5

The SiteMap control available in ASP.NET 3.5 provides a site navigation view in a hirerchichal format. The data source for the SiteMap control is an XML file that has name and URL of the site navigation.

The starting point in site map based navigation is the site map provider. ASP.NET ships with a single site map provider, named XMLSiteMapProvider which is able retrieve site map information from as XML file. If you want to retrieve a site map from another location or in a custom format, you will need to create your own site map provider.

The XmlSiteMapProvider looks for a file named Web.sitemap in the root of the virtual directory. Like all site map providers , its task is to extract the site map data and create the corresponding SiteMap object. This SiteMap object is then made available to other control through the SiteMapDataSource.

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.

OK, let's get started. Create an ASP.NET application using Visual Studio 2008.

Now add a Site Map file to the project by right clicking on the project name, select Add New Item and select Site Map from the templates.


Figure 1.

After adding a Site Map file, we need to add Site Map Nodes represeted by siteMapNode XML elements as shown below.

Here is my final Web2.sitemap looks like. The siteMapNode has a title and a url attribute that represent the title and URL of the link.

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="" >

    <siteMapNode url="" title="Modeling Corner"  description="This is modeling site">

            <siteMapNode title="Header"  description="This is modeling corner header">

            <siteMapNode url="" title="Models"  description="This is model section">

                  <siteMapNode url="" title="Men Models"  description="This section is for men models" />

                  <siteMapNode url="" title="Women Models"  description="This section is for women models" />

                  <siteMapNode url="" title="Teen Models"  description="This section is for teen models" />

                  <siteMapNode url="" title="Kid Models"  description="This section is for kid models" />

                  <siteMapNode url="" title="Baby Models"  description="This section is for baby models" />


            <siteMapNode url="" title="Photographers"  description="This is photographer section"></siteMapNode>

            <siteMapNode url="" title="Agencies"  description="This is agencies section"></siteMapNode>

            <siteMapNode url="" title="Blogs"  description="This is blogs section"></siteMapNode>

            <siteMapNode url="" title="News"  description="This is news section"></siteMapNode>

            <siteMapNode url="" title="Castings"  description="This is castings section"></siteMapNode>

            <siteMapNode url="" title="Videos"  description="This is videos section"></siteMapNode>

            <siteMapNode url="" title="Forums"  description="This is forum section"></siteMapNode>


            <siteMapNode title="Footer" url="" description="This is modeling corner footer">

                  <siteMapNode url="" title="Hosted By MaximumASP"  description="This is host website name"></siteMapNode>

                  <siteMapNode url="" title="Home"  description="This is home page name"></siteMapNode>

                  <siteMapNode url="" title="Contact Us"  description="This is contact us page"></siteMapNode>

                  <siteMapNode url="" title="About Us"  description="This is about us page"></siteMapNode>

                  <siteMapNode url="" title="Help"  description="This is help page"></siteMapNode>

                  <siteMapNode url="" title="Search"  description="This is search page"></siteMapNode>



      <!-- -->



Now let's add a SiteMapDataSource control to the ASP.NET page. This is my page page looks like.

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

        <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"

            BackColor="#F7F6F3" DynamicHorizontalOffset="2" Font-Names="Verdana"

            Font-Size="14px" ForeColor="#7C6F57" StaticSubMenuIndent="14px">

            <StaticSelectedStyle BackColor="#5D7B9D" />

            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />

            <DynamicHoverStyle BackColor="#7C6F57" ForeColor="White" />

            <DynamicMenuStyle BackColor="#F7F6F3" />

            <DynamicSelectedStyle BackColor="#5D7B9D" />

            <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />

            <StaticHoverStyle BackColor="#7C6F57" ForeColor="White" />



Now if you run and look at the output, you will see output looks like following where you can click on the root node and it will display next level nodes and so on.


Download the attached source code for more details.


Up Next
    Ebook Download
    View all
    View all