Dynamic sitemaps

Shad Storhaug edited this page Feb 9, 2014 · 1 revision

Want to contribute? See our Contributing to MvcSiteMapProvider guide.



Version 4.x Documentation


Unofficial Documentation and Resources

Other places around the web have some documentation that is helpful for getting started and finding answers that are not found here.

Tutorials and Demos

Version 4.x
Version 3.x

Forums and Q & A Sites

Other Blog Posts

Clone this wiki locally

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 included 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 (or in attributes). This node will serve as a template and tell the MvcSiteMapProvider infrastructure to use a custom dynamic node provider:

Next, add dynamicNodeProvider attribute to your newly created sitemap node in the following format: Fully.Qualified.Class.Name, AssemblyName. Note that the dynamicNodeProvider is ignored in the root node.

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

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection() 
    { 
        // Build value 
        var returnValue = new List<DynamicNode>();

        // Create a node for each album 
        foreach (var album in storeDB.Albums.Include("Genre")) 
        { 
            DynamicNode node = new DynamicNode(); 
            node.Title = album.Title; 
            node.ParentKey = "Genre_" + album.Genre.Name; 
            node.RouteValues.Add("id", album.AlbumId);

            returnValue.Add(node); 
        }

        // Return 
        return 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();

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection() 
    { 
        // ... 
    } 

    public override CacheDescription GetCacheDescription() 
    { 
        return new CacheDescription("StoreDetailsDynamicNodeProvider") 
        { 
            SlidingExpiration = TimeSpan.FromMinutes(1) 
        }; 
    } 
}