Implementing ASP.NET Custom Site-Map
In this blog I am trying to Implementing ASP.NET
A site map (or sitemap) is a list of web pages of a web site
that are accessed by the users. ASP.NET site navigation provides several
Web server controls that display a navigation structure on a Web page: the
SiteMapPath, TreeView, and Menu controls that are.
An XML Sitemap is a structured format that a user doesn't need
to see, but it tells the search engine about the pages in your site, their
relative importance to each other, and how often (frequently) they are updated.
HTML sitemaps are basically designed for the user to help
them to find the content on the page, and they don't need to include each and
every subpage. This is very helpful to visitors and search engine to find pages
on the site.
XML is a document structure and encoding standard used, amongst
many other things, as the standard for web crawlers to find and parse sitemaps.
Site maps can improve search engine optimization of a site by making sure that
all the pages can be found.
ASP.NET Site map
This provides default site map for navigation of web site.
In ASP.NET the site navigation provides many Web server controls that can
display a navigation structure on a Web page: that are SiteMapPath, TreeView,
and Menu controls .These all Web server controls are used by
the ASP.NET default site-map provider, or the XmlSiteMapProvider class,
to access site-map information from the XML-formatted Web.sitemap file.
However you can implement your own custom site-map provider,
according to your requirement. There are basically three primary reasons for
which we need to create a custom site-map provider are as follows:
1. First is that if you want to store
site-map information in a data source, then with the default implementation of
ASP.NET site-map provider it could not be stored, because it is not supported
by default implementation. For example, you might want to store your site-map
data in a Visual FoxPro database, an Oracle database, or other data source.
2. Second is to manage navigation
information using a schema that is different from the schema used for the Web.
Sitemap file. For example, you might have an existing implementation for
storing site-map data.
use a dynamic site-map structure. For example, you might want each client
account to be able to view a different site map.
Creating a Custom Site Map Provider
Setp1: After starting your project, on solution explorers and adds a
class and name it as”CounstomSiteProvider” and write down the following
To accomplish this we need to inherit the StaticSiteMapProvider class
and then implement our custom functionality in the class by method overriding
which is provided by this base class. There are two methods that we need to override
which are BuildSiteMap and GetRootNodeCore. The
first is building the site map and the second returns the root node of the
built site map.
public class CustomSiteMapProvider : StaticSiteMapProvider
private readonly object _siteMapLocking = new object();
private SiteMapNode _siteMapRootNode;
method is responsible for creating whole site map
/// <returns> return
the couston sitemap for your site </returns>
public override SiteMapNode BuildSiteMap()
// Use a
lock to provide thread safety
if (_siteMapRootNode != null)
protected override SiteMapNode GetRootNodeCore()
root node of your sitemap
this project it sets the default.aspx as root node in sitemap
private void CreateSiteMapRoot()
_siteMapRootNode = new SiteMapNode(this, "Root", "~/Default.aspx", "Root");
method create all chiled node of root node
private void CreateSiteMapNodes()
SiteMapNode node = null;
for (int i = 1; i <= 3; i++)
node = new SiteMapNode(this,
Step 2: Now deploying the Custom Site Map Provider in web.Config file.
<compilation debug="true" targetFramework="4.0" />
<siteMap defaultProvider="CustomSiteMapProvider" enabled="true">
<add name="CustomSiteMapProvider" type="WebApplication1.CustomSiteMapProvider"/>
Step 3:Now add a site1.master page in your project and
adds number of content pages you want to add in your website.
Step 4: Open site1.master and add treeView control from
tool box and select data source attribute(from design view you can select
Step 5: Choose SiteMap and click on ok button.
Step 6: The source of site1.master will be displayed as follow.
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<asp:ContentPlaceHolder ID="head" runat="server">
<form id="form1" runat="server">
<table border="1" width="100%">
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
Step 7: Finally build and run your project the following navigation (sitemap will be display)
This article is intended to explore the basic pathways toward
the custom sitemap creation; however you can create more complicated/according
to your need. But this article is only for first basic steps for creating the
I hope you have learned how to do so very precisely
understand the basics of custom site mapping.