-->

How to implement SiteMapPath in asp.net

Introduction

In this post, I will show you how to implement SiteMapPath in asp.net.

The SiteMapPath control is a navigation control that allows the user to know the current page location with respect to the home page. 


Steps :

Just follow the steps and get result easily.

Step - 1 : Create New Project

Go to File > New > Project > Select asp.net mvc4 web application > Entry Application Name > Click OK.

Step-2: Add some pages to project.

Go to Solution Explorer > Right Click on Project > Add > New item > Select Web Form using Master page > Enter Page name >Add.

Here in this example i have added pages followings...
  • Home.aspx
  • About.aspx
  • MainPage.aspx
  • SubPage1.aspx
  • SubPage2.aspx
Menu Structure like


Step-3: Add a Sitemap file.

Go to Solution Explorer > Right Click on Project > Add > New item > Select "Site Map" File > Enter Page name >Add.

Write below text (Content as your Site Structure) to SiteMap file.
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="Default.aspx" title="Home"  description="Home">
      <siteMapNode url="About.aspx" title="About"  description="About" />
      <siteMapNode url="MainPage.aspx" title="Main Page"  description="Main Page">
        <siteMapNode url="SubPage1.aspx" title="Sub Page 1" description="Sub Page 1"/>
        <siteMapNode url="SubPage2.aspx" title="Sub Page 2" description="Sub Page 2"/>
      </siteMapNode>
    </siteMapNode>
</siteMap>

Step-4: Modify web.config

Add this inside System.web file.

<siteMap>
      <providers>
        <add name="MySiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="~/MySiteMap.sitemap"/>
      </providers>

    </siteMap>

Step-5: Add SiteMapDataSource control into site.master page.

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

Step-6: Add Menu control in Master page.

<asp:Menu ID="Menu1" runat="server" CssClass="menu"
                    EnableViewState="false" IncludeStyleBlock="false"  StaticDisplayLevels="2"
                    Orientation="Horizontal" DataSourceID="SiteMapDataSource1" >
                </asp:Menu>

Step-7: Add SitemapPath control into Master page.


<asp:SiteMapPath ID="SiteMapPath1" runat="server" SiteMapProvider="MySiteMap" Font-Names="Verdana" Font-Size="0.8em" PathSeparator=" > ">
                    <CurrentNodeStyle ForeColor="#333333" />
                    <NodeStyle Font-Bold="True" ForeColor="#990000" />
                    <PathSeparatorStyle Font-Bold="True" ForeColor="#990000" />
                    <RootNodeStyle Font-Bold="True" ForeColor="#FF8000" />
                </asp:SiteMapPath>

Step-8: Run Application

Click on Menu Link and Check your SiteMap path.

Related Post:

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.