Click here to Skip to main content
15,867,330 members
Articles / Web Development / ASP.NET
Article

Building a dynamic SiteMap in ASP.NET 2.0 for a large website

Rate me:
Please Sign up or sign in to vote.
4.27/5 (20 votes)
21 Dec 20063 min read 349.9K   7.2K   83   67
Practical approach to building a dynamic site map on a large website in ASP.NET.

dynamic sitemap rendered

Introduction

Sitemaps and breadcrumbs (SiteMapPath) are useful and easy to implement for a static site with a sitemap file. For dynamic sites, something as simple seems to get much more complicated.

When I started reading about sitemaps for dynamic sites, I found a common approach: generate a static site map for the whole website from, for example, a data source. Re-generate periodically. Use the XmlSiteMapProvider. Cache. The technique is described in this CodeProject article.

This doesn't work for my site at all. I need to provide a site map for a large number of pages, potentially hundreds of thousands. The site is deep and dynamic, and only a small unpredictable set of pages is accessed frequently. There're also two hundred different object types in the back-end database which is not coupled with the user-interface at all. Querying everything once is not trivial. Generating an accurate site map for the whole site would take too long, would be a lot of code, and would definitely take too much memory to cache.

This article demonstrates a simpler and practical solution chosen. You can see it working live on www.foodcandy.com.

Architecture

The core of the architecture is a relatively simple dynamic data provider, SiteMapDataProvider, based on the StaticSiteMapProvider. The provider is used as the default provider throughout the application, and can stack nodes that appear in the site map path. Each document that renders dynamic content stacks itself with the appropriate hierarchy of parent nodes.

Implementation

The SiteMapDataProvider is straightforward. It will create a root node, and can stack a node behind any existing node, creating a path.

C#
public class SiteMapDataProvider : StaticSiteMapProvider
{
 private SiteMapNode mRootNode = null;

 ...

 // create the root node
 public override void Initialize(string name, 
        NameValueCollection attributes)
 {
     base.Initialize(name, attributes);
     mRootNode = new SiteMapNode(this, "Home", 
                 "Default.aspx", "Home");
     AddNode(mRootNode);
 }

 public override SiteMapNode FindSiteMapNode(string rawUrl)
 {
     return base.FindSiteMapNode(rawUrl);
 }

 // stack a node under the root
 public SiteMapNode Stack(string title, string uri)
 {
     return Stack(title, uri, mRootNode);
 }

 // stack a node under any other node
 public SiteMapNode Stack(string title, string uri, 
                    SiteMapNode parentnode)
 {
     lock (this)
     {
         SiteMapNode node = base.FindSiteMapNodeFromKey(uri);

         if (node == null)
         {
             node = new SiteMapNode(this, uri, uri, title);
             node.ParentNode = 
               ((parentnode == null) ? mRootNode : parentnode);
             AddNode(node);
         }
         else if (node.Title != title)
         {<BR>             // support renaming documents
             node.Title = title;
         }
  
         return node;
     }
 }
}

I have put the above implementation in a DBlock.SiteMapProvider.dll assembly, and have referenced it in web.config as the default provider.

XML
<?xml version="1.0"?>
<configuration>
 <system.web>
  <siteMap enabled="true" 
            defaultProvider="SiteMapDataProvider"> 
   <providers>
    <add name="SiteMapDataProvider" 
       type="DBlock.SiteMapDataProvider, DBlock.SiteMapDataProvider" />
   </providers>
  </siteMap>
 </system.web>
</configuration>

I've added a simple master page with a SiteMapPath and a default page. This yields a Home (root) site map node on Default.aspx.

Now remember, the goal is to have a dynamic site map. I've added a button that redirects to Default.aspx?id=<guid> for demo purposes. To display the site map accordingly, I've built a linked list of site map nodes, then stacked them into the site map.  Below is the Default.aspx Page_Load and one more helper function in SiteMapDataProvider to do the stack.

C#
string id = Request["id"];
if (!string.IsNullOrEmpty(id))
{
  List<KeyValuePair<string, Uri>> nodes = 
     new List<KeyValuePair<string, Uri>>();
  nodes.Add(new KeyValuePair<string, 
     Uri>("Dynamic Content", new Uri(Request.Url, "Default.aspx?id=")));
  nodes.Add(new KeyValuePair<string, Uri>(Request["id"], Request.Url));
  ((SiteMapDataProvider) SiteMap.Provider).Stack(nodes);
}
C#
public void Stack(List<KeyValuePair<string, Uri>> nodes)
{ 
  SiteMapNode parent = RootNode; 
  foreach (KeyValuePair<string, Uri> node in nodes)
  {
   parent = Stack(node.Key, node.Value.PathAndQuery, parent); 
  } 
} 

Points of Interest

This works very well for pages that retrieve dynamic content. My project typically retrieves objects from the database or cache, and displays them on each page. Adding two-three lines of code to each page generates the complete site map of pages that are actually being hit.

The obvious disadvantage of this technique is that you have to add code in each page to generate the site map, and that you must carefully track the URL of your intermediate nodes (the Default.aspx?id=, with a blank ID, in the example above) to keep things consistent. It would be great to describe the relationship between dynamic pages in some other form that can be verified at compile time. Maybe, a better (yet more complex) approach is to extend the XML provider to allow dynamic binding in nodes?

You may also want to limit the size of the site map for large sites to avoid consuming too much memory. Simply clear the site map once it has reached your size limit.

History

  • 2006/12/16: Initial release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
dB.
Team Leader Application Security Inc., www.appsecinc.com
United States United States
Daniel Doubrovkine has been in software engineering for twelve years and is currently development manager at Application Security Inc. in New York City. He has been involved in many software ventures, including Xo3 and Vestris Inc, was a development lead at Microsoft Corp. in Redmond, and director of Engineering at Visible Path Corp. in New York City. Daniel also builds and runs a foodie website, http://www.foodcandy.com.

Comments and Discussions

 
QuestionEdit sitemap on runtime Pin
Nithesh AN8-Jul-13 3:23
Nithesh AN8-Jul-13 3:23 
AnswerRe: Edit sitemap on runtime Pin
dB.8-Jul-13 10:38
dB.8-Jul-13 10:38 
QuestionDBLock.SiteMapdataProvider - Need function parameters Pin
Member 796913914-Feb-13 7:00
Member 796913914-Feb-13 7:00 
GeneralMy vote of 5 Pin
Pritesh Aryan31-Jul-12 0:25
Pritesh Aryan31-Jul-12 0:25 
GeneralMy vote of 1 Pin
danialram10-Apr-11 20:24
danialram10-Apr-11 20:24 
GeneralRegenerate Sitemap Pin
Sherwin Valdez6-Mar-11 19:34
Sherwin Valdez6-Mar-11 19:34 
GeneralRe: Regenerate Sitemap Pin
Member 1058794911-Feb-14 1:52
Member 1058794911-Feb-14 1:52 
GeneralGenerate a SiteMap Pin
olehaugen7-Feb-11 12:38
olehaugen7-Feb-11 12:38 
QuestionCan I just use the source code in other project rather than using the dll Pin
hon1234564-Jan-10 18:09
hon1234564-Jan-10 18:09 
AnswerRe: Can I just use the source code in other project rather than using the dll Pin
dB.7-Jan-10 0:31
dB.7-Jan-10 0:31 
QuestionHow to create sitemap using your code with url rewriting Pin
Ravenet26-Dec-09 17:28
Ravenet26-Dec-09 17:28 
AnswerRe: How to create sitemap using your code with url rewriting Pin
dB.7-Jan-10 0:34
dB.7-Jan-10 0:34 
GeneralRe: How to create sitemap using your code with url rewriting Pin
Ravenet7-Jan-10 2:33
Ravenet7-Jan-10 2:33 
GeneralRe: How to create sitemap using your code with url rewriting Pin
dB.7-Jan-10 3:48
dB.7-Jan-10 3:48 
GeneralI used your codes in my project but sitemappath is not correct Pin
osmanimamoglu18-Dec-09 4:31
osmanimamoglu18-Dec-09 4:31 
Question[Message Deleted] Pin
Chidambaram Narayanan26-Nov-09 2:57
Chidambaram Narayanan26-Nov-09 2:57 
AnswerRe: Request["id"] is always null. Pin
dB.28-Nov-09 3:09
dB.28-Nov-09 3:09 
GeneralError in Visual Basic (CAST) Pin
rserven4-Sep-09 8:58
rserven4-Sep-09 8:58 
GeneralRe: Error in Visual Basic (CAST) Pin
rserven4-Sep-09 10:15
rserven4-Sep-09 10:15 
GeneralAdding mutliple breadcrumbs. Pin
djremix873-Mar-09 7:57
djremix873-Mar-09 7:57 
GeneralRe: Adding mutliple breadcrumbs. Pin
dB.3-Mar-09 10:10
dB.3-Mar-09 10:10 
GeneralSitemap binded to a TreeView control Pin
emipasat23-Jan-09 5:06
emipasat23-Jan-09 5:06 
QuestionConverted to VB? Pin
vsammons23-Oct-08 16:57
vsammons23-Oct-08 16:57 
AnswerRe: Converted to VB? Pin
dB.24-Oct-08 2:45
dB.24-Oct-08 2:45 
GeneralRe: Converted to VB? Pin
vsammons24-Oct-08 2:49
vsammons24-Oct-08 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.