How to define a site map in ASP.NET

ASP.NET ships with a single site map provider, named XmlSiteMapProvider which looks for an XML file named Web.sitemap in the root of the virtual directory and retrieves site map information from it. If you want to retrieve a site map from another location or in a custom format, you’ll need to create your own site map provider. Like all site map providers, its task is to extract the site map data and create the corresponding SiteMap object which is then is available to other controls through the SiteMapDataSource. To try this, you need to begin by creating a Web.sitemap file and defining the website structure using the <siteMap> and <siteMapNode> elements. To add a site map using Visual Studio 2010, choose Website ➤ Add New Item (or Project ➤ Add New Item in a web project), choose the Site Map template, and then click Add. The next lines show the bare-bones structure that the site map file uses:

 

<?xml version=”1.0″ encoding=”utf-8″ ?>

<siteMap xmlns=”http://schemas.microsoft.com/AspNet/SiteMap-File-1.0″ >

<siteMapNode url=”” title=””  description=””>

<siteMapNode url=”” title=””  description=”” />

<siteMapNode url=”” title=””  description=”” />

</siteMapNode>

</siteMap>

 

Your site map must begin with the root <siteMap> node, followed by a single <siteMapNode> element, representing the default home page. You can nest other <siteMapNode> elements in the root <siteMapNode> as many layers deep as you want. Each site map node should have a title, description, and URL, as shown here:

 

<siteMapNode title=”Home” description=”Home” url=”./default.aspx”>

 

Important notes:

1. In the example, the URL uses the ./ relative path syntax, which indicates the root of the web application. This style isn’t necessary, but it is strongly recommended, as it ensures that your site map links are interpreted correctly regardless of the current folder.

2. You should use the <siteMapNode> to create a site map. You can’t create two site map nodes with the same URL, because the XmlSiteMapProvider uses the URL as a unique key. If you create your own site map provider or use a third-party provider, you may allow duplicate URLs and require separate key information.

 

The next lines show a sample site map:

 

<?xml version=”1.0″ encoding=”utf-8″ ?>

<siteMap xmlns=”http://schemas.microsoft.com/AspNet/SiteMap-File-1.0″ >

<siteMapNode title=”Home” description=”Home” url=”./default.aspx”>

<siteMapNode title=”Web development” description=”Web development” url=”./WebDevelopment.aspx”>

<siteMapNode title=”HTML and CSS” description=”HTML and CSS” url=”./HTMLandCSS.aspx” />

<siteMapNode title=” Scripting” description=”Scripting” url=”./Scripting.aspx” />

</siteMapNode>

<siteMapNode title=”Enterprise servers and development” description=” Enterprise servers and development we offer” url=”./EnterpriseSD.aspx”>

<siteMapNode title=”Mail servers” description = “Mail servers” url=”./MailServers.aspx” />

<siteMapNode title=”SQL servers” description=”SQL servers” url=”./SQLServers.aspx” />

<siteMapNode title=”Application servers” description=”Application servers” url=”./ApplicationServers.aspx” />

</siteMapNode>

</siteMap>