Click here to Skip to main content
Click here to Skip to main content

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

, 21 Dec 2006
Rate this:
Please Sign up or sign in to vote.
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.

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

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);
}
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

Share

About the Author

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 PinprofessionalNithesh AN8-Jul-13 3:23 
AnswerRe: Edit sitemap on runtime PinmemberdB.8-Jul-13 10:38 
QuestionDBLock.SiteMapdataProvider - Need function parameters PinmemberMember 796913914-Feb-13 7:00 
GeneralMy vote of 5 PinmemberPritesh Aryan31-Jul-12 0:25 
GeneralMy vote of 1 Pinmemberdanialram10-Apr-11 20:24 
GeneralRegenerate Sitemap PinmemberSherwin Valdez6-Mar-11 19:34 
GeneralRe: Regenerate Sitemap PinmemberMember 1058794911-Feb-14 1:52 
GeneralGenerate a SiteMap Pinmemberolehaugen7-Feb-11 12:38 
QuestionCan I just use the source code in other project rather than using the dll Pinmemberhon1234564-Jan-10 18:09 
AnswerRe: Can I just use the source code in other project rather than using the dll PinmemberdB.7-Jan-10 0:31 
QuestionHow to create sitemap using your code with url rewriting PinmemberRavenet26-Dec-09 17:28 
AnswerRe: How to create sitemap using your code with url rewriting PinmemberdB.7-Jan-10 0:34 
GeneralRe: How to create sitemap using your code with url rewriting PinmemberRavenet7-Jan-10 2:33 
GeneralRe: How to create sitemap using your code with url rewriting PinmemberdB.7-Jan-10 3:48 
GeneralI used your codes in my project but sitemappath is not correct Pinmemberosmanimamoglu18-Dec-09 4:31 
Question[Message Deleted] PinmemberChidambaram Narayanan26-Nov-09 2:57 
AnswerRe: Request["id"] is always null. PinmemberdB.28-Nov-09 3:09 
GeneralError in Visual Basic (CAST) PinmemberMember 40433464-Sep-09 8:58 
GeneralRe: Error in Visual Basic (CAST) PinmemberMember 40433464-Sep-09 10:15 
GeneralAdding mutliple breadcrumbs. Pinmemberdjremix873-Mar-09 7:57 
GeneralRe: Adding mutliple breadcrumbs. PinmemberdB.3-Mar-09 10:10 
GeneralSitemap binded to a TreeView control Pinmemberemipasat23-Jan-09 5:06 
QuestionConverted to VB? Pinmembervsammons23-Oct-08 16:57 
AnswerRe: Converted to VB? PinmemberdB.24-Oct-08 2:45 
GeneralRe: Converted to VB? Pinmembervsammons24-Oct-08 2:49 
GeneralRe: Converted to VB? PinmemberdB.24-Oct-08 5:18 
Question[Message Deleted] Pinmemberjlimaco22-Feb-08 18:57 
GeneralRe: It does not show the root node PinmemberdB.23-Feb-08 1:27 
QuestionRe: It does not show the root node Pinmemberjlimaco25-Feb-08 16:24 
GeneralRe: It does not show the root node PinmemberdB.2-Mar-08 4:58 
GeneralOptimize Memory Usage Pinmemberpbzzbp12-Dec-07 3:47 
"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."
 
Another approach would be to override FindSiteMapNode and return the node from there rather then add to the site's tree. When we add to the site tree the tree will continue to grow, and if you have a large site this will not be practical. This should do the trick:
 

      public override SiteMapNode FindSiteMapNode(string rawUrl)
      {
         // is the node already in the tree
         SiteMapNode node = base.FindSiteMapNode(rawUrl);
 
         if (node == null && rawUrl=="/some_form.aspx")
         {
            node = new SiteMapNode(this, "", "/some_form.aspx", "Test");
            node.ParentNode = root;
 
            // do not add to the tree if this is a dynamic node and doesn't need to be cached
            //AddNode(node);
         }
         return node;
      }
 
Of course the part where we check for some_form.aspx will be different -- this is to keep it simple.
GeneralRe: Optimize Memory Usage PinmemberdB.28-Jan-08 18:21 
GeneralRe: Optimize Memory Usage Pinmemberpbzzbp23-Mar-08 8:09 
GeneralCould not load type 'App_Code' from assembly 'App_Code'.error in siteMapDataProvider [modified] Pinmemberharmeetp29-Nov-07 9:45 
GeneralRe: Could not load type 'App_Code' from assembly 'App_Code'.error in siteMapDataProvider PinmemberdB.28-Jan-08 18:22 
Questionuser specific dynamic menu PinmemberMarianaZ24-Nov-07 20:44 
AnswerRe: user specific dynamic menu PinmemberdB.25-Nov-07 3:20 
GeneralCould not load type 'SqlSiteMapProvider' Error Pinmemberalexaz126-Oct-07 6:10 
GeneralRe: Could not load type 'SqlSiteMapProvider' Error PinmemberdB.30-Oct-07 3:26 
GeneralRe: nice sir ...source code running...1 more request PinmemberdB.30-Sep-07 3:44 
GeneralRe: nice sir ...source code running...1 more request Pinmembersahil31_mohali30-Sep-07 3:53 
GeneralRe: nice sir ...source code running...1 more request PinmemberdB.30-Sep-07 3:54 
Generalyour code is running...but one question Pinmembersahil31_mohali30-Sep-07 22:23 
GeneralRe: your code is running...but one question PinmemberdB.1-Oct-07 1:42 
General[Message Deleted] Pinmembersahil31_mohali30-Sep-07 3:26 
GeneralRe: Error:From where u inherit StaticSiteMapProvider PinmemberdB.30-Sep-07 3:27 
GeneralRe: Error:From where u inherit StaticSiteMapProvider PinmemberdB.30-Sep-07 3:29 
GeneralRe: again problem PinmemberdB.30-Sep-07 3:20 
GeneralRe: problem with build dll PinmemberdB.30-Sep-07 2:38 
GeneralBrilliant | Cause of confusion [modified] PinmemberGhostnet22-Sep-07 19:16 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 21 Dec 2006
Article Copyright 2006 by dB.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid