Skip to content
JohannesEH edited this page Dec 15, 2011 · 7 revisions

In many web applications, sitemap nodes are directly related to content in a persistent store like a database.For example, in an e-commerce application, a list of product details pages in the sitemap maps directly to the list of products in the database. Using dynamic sitemaps, a small class can be provided to the MvcSiteMapProvider offering a list of dynamic nodes that should be incldued in the sitemap. This ensures the product pages do not have to be specified by hand in the sitemap XML.

First of all, a sitemap node should be defined in XML. This node will serve as a template and tell the MvcSiteMapProvider infrastructure to use a custom dynamic node provider:

<mvcSiteMapNode title="Details" action="Details" dynamicNodeProvider="MvcMusicStore.Code.StoreDetailsDynamicNodeProvider, MvcMusicStore" />


Next, a class implementing MvcSiteMapProvider.Extensibility.IDynamicNodeProvider or extending MvcSiteMapProvider.Extensibility.DynamicNodeProviderBase should be created in your application code. Here’s an example:

public class StoreDetailsDynamicNodeProvider 
    : DynamicNodeProviderBase 
{ 
    MusicStoreEntities storeDB = new MusicStoreEntities();
<span style="color: blue;">public</span> <span style="color: blue;">override</span> IEnumerable&lt;DynamicNode&gt; GetDynamicNodeCollection() 
{ 
    <span style="color: green;">// Build value </span>
    <span style="color: blue;">var</span> returnValue = <span style="color: blue;">new</span> List&lt;DynamicNode&gt;();

    <span style="color: green;">// Create a node for each album </span>
    <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> album <span style="color: blue;">in</span> storeDB.Albums.Include(<span style="color: #a31515;">"Genre"</span>)) 
    { 
        DynamicNode node = <span style="color: blue;">new</span> DynamicNode(); 
        node.Title = album.Title; 
        node.ParentKey = <span style="color: #a31515;">"Genre_"</span> + album.Genre.Name; 
        node.RouteValues.Add(<span style="color: #a31515;">"id"</span>, album.AlbumId);

        returnValue.Add(node); 
    }

    <span style="color: green;">// Return </span>
    <span style="color: blue;">return</span> returnValue; 
} 

}

Cache dependency

When providing dynamic sitemap nodes to the MvcSiteMapProvider, chances are that the hierarchy of nodes will become stale, for example when adding products in an e-commerce website. This can be solved by specifying a CacheDescriptor on your MvcSiteMapProvider.Extensibility.IDynamicNodeProvider implementation:

public class StoreDetailsDynamicNodeProvider 
    : DynamicNodeProviderBase 
{ 
    MusicStoreEntities storeDB = new MusicStoreEntities();
<span style="color: blue;">public</span> <span style="color: blue;">override</span> IEnumerable&lt;DynamicNode&gt; GetDynamicNodeCollection() 
{ 
    <span style="color: green;">// ... </span>
} 

<span style="color: blue;">public</span> <span style="color: blue;">override</span> CacheDescription GetCacheDescription() 
{ 
    <span style="color: blue;">return</span> <span style="color: blue;">new</span> CacheDescription(<span style="color: #a31515;">"StoreDetailsDynamicNodeProvider"</span>) 
    { 
        SlidingExpiration = TimeSpan.FromMinutes(1) 
    }; 
} 

}