Home > DeveloperSection > Blogs > Implementing ASP.NET Custom Site-Map

Implementing ASP.NET Custom Site-Map


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 2982  View(s)
Rate this:

Implementing ASP.NET Custom Site-Map


In this blog I am trying to Implementing ASP.NET Custom Site-Map.

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.

XML Sitemap

 

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 Sitemap

 

 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.

3.   To 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



Setp1After starting your project, on solution explorers and adds a class and name it as”CounstomSiteProvider” and write down the following codes.

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.

 

using System.Web;

namespace WebApplication1

{

    public class CustomSiteMapProvider : StaticSiteMapProvider

    {

        #region Members

        private readonly object _siteMapLocking = new object();

        private SiteMapNode _siteMapRootNode;

        #endregion

        #region Methods

        /// <summary>

        /// this method is responsible for creating whole site map

        /// </summary>

        /// <returns> return the couston sitemap for your site </returns>

        public override SiteMapNode BuildSiteMap()

        {

            // Use a lock to provide thread safety

            lock (_siteMapLocking)

            {

                if (_siteMapRootNode != null)

                {

                    return _siteMapRootNode;

                }

                base.Clear();

                CreateSiteMapRoot();

                CreateSiteMapNodes();

                return _siteMapRootNode;

            }

        } 

      

        protected override SiteMapNode GetRootNodeCore()

        {

            return BuildSiteMap();

        }

        /// <summary>

        /// create root node of your sitemap

        /// in this project it sets the default.aspx as root node in sitemap

        /// </summary>

        private void CreateSiteMapRoot()

        {

            _siteMapRootNode = new SiteMapNode(this"Root""~/Default.aspx""Root");

            AddNode(_siteMapRootNode);

        }

        /// <summary>

        /// this method create all chiled node of root node

        /// </summary>

        private void CreateSiteMapNodes()

        {

            SiteMapNode node = null;

            for (int i = 1i <= 3i++)

            {

                node = new SiteMapNode(this,

                    string.Format("Child{0}"i),

                    string.Format("~/WebForm{0}.aspx"i),

                    string.Format("Child{0}"i));

                AddNode(node_siteMapRootNode);

            }

        }

        #endregion

    }

}

 

Step 2:  Now deploying the Custom Site Map Provider in web.Config file. As

<system.web>

    <compilation debug="true" targetFramework="4.0" />

    <siteMap defaultProvider="CustomSiteMapProvider" enabled="true">

      <providers>

        <clear/>

        <add name="CustomSiteMapProvider" type="WebApplication1.CustomSiteMapProvider"/>

      </providers>

    </siteMap>

  </system.web>

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 it).


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">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <asp:ContentPlaceHolder ID="head" runat="server">

    </asp:ContentPlaceHolder>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <table border="1" width="100%">

            <tr>

                <td height="100px">

         <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">

                    </asp:TreeView>

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

                </td>

            </tr>

        </table>

        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>

    </div>

    </form>

</body>

</html>


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 custom sitemaps.

 I hope you have learned how to do so very precisely understand the basics of custom site mapping.

Thanks.


Don't want to miss updates? Please click the below button!

Follow MindStick