-
Notifications
You must be signed in to change notification settings - Fork 0
Dynamic sitemaps
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. 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();
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)
};
}
}