Sunday, January 15, 2012

dynamic sitemap in mvc 3


Getting the bits

As always, the bits are available on CodePlex: MvcSiteMapProvider 2.0.0
If you prefer to have the full source code, download the example application or check the source code tab on CodePlex.

Introduction


MvcSiteMapProvider is, as the name implies, an ASP.NET MVC SiteMapProvider implementation for the ASP.NET MVC framework. Targeted at ASP.NET MVC 2, it provides sitemap XML functionality and interoperability with the classic ASP.NET sitemap controls, like the SiteMapPath control for rendering breadcrumbs and the Menu control.
Based on areas, controller and action method names rather than hardcoded URL references, sitemap nodes are completely dynamic based on the routing engine used in an application. The dynamic character of ASP.NET MVC is followed in the MvcSiteMapProvider: there are numerous extensibility points that allow you to extend the basic functionality offered.


"MvcSiteMapProvider" enabled="true">
 
   
    "MvcSiteMapProvider"
         type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider"
         siteMapFile="~/Mvc.Sitemap"
         securityTrimmingEnabled="true"
         enableLocalization="true"
         scanAssembliesForSiteMapNodes="true"
         skipAssemblyScanOn=""
         attributesToIgnore="bling"
         nodeKeyGenerator="MvcSiteMapProvider.DefaultNodeKeyGenerator, MvcSiteMapProvider"
         controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver, MvcSiteMapProvider"
         actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver, MvcSiteMapProvider"
         aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider"
         />
 

in site map





   
   
   
   
   
   
 
    dynamicNodeProvider="WhoTrackYou.Models.StoreDetailsDynamicNodeProvider, WhoTrackYou"/>
 

in page where you want to show site map use following function:

  • Html.MvcSiteMap().Menu() - Can be used to generate a menu
  • Html.MvcSiteMap().SubMenu() - Can be used to generate a submenu
  • Html.MvcSiteMap().SiteMap() - Can be used to generate a list of all pages in your sitemap
  • Html.MvcSiteMap().SiteMapPath() - Can be used to generate a so-called "breadcrumb trail"
  • Html.MvcSiteMap().SiteMapTitle() - Can be used to render the current SiteMap node's title 




The following attributes can be given on an XML node element:
Attribute Required? Default Description
title Yes (empty) The title of the node.
description No (empty) Description of the node.
area No (empty) The MVC area for the sitemap node. If not specified, it will be inherited from a node higher in the hierarchy.
controller Yes (empty) The MVC controller for the sitemap node. If not specified, it will be inherited from a node higher in the hierarchy.
action Yes (empty) The MVC action method for the sitemap node. If not specified, it will be inherited from a node higher in the hierarchy.
key No (autogenerated) The unique identifier for the node.
url No (autogenerated based on routes) The URL represented by the node.
roles No (empty) Comma-separated list of roles allowed to access the node and its child nodes.
resourceKey No (empty) Optional resource key.
clickable No True Is the node clickable or just a grouping node?
targetFrame No (empty) Optional target frame for the node link.
imageUrl No (empty) Optional image to be shown by supported HtmlHelpers.
lastModifiedDate No (empty) Last modified date for the node.
changeFrequency No Undefined Change frequency for the node.
updatePriority No Undefined Update priority for the node.
dynamicNodeProvider No (empty) A class name implementing MvcSiteMapProvider.Extensibility.IDynamicNodeProvider and providing dynamic nodes for the site map.



 public class StoreDetailsDynamicNodeProvider
      : DynamicNodeProviderBase
    {
        WhoTracksMeEntities storeDB = new WhoTracksMeEntities();

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

            // Create a node for each Cookie
            foreach (var ck in storeDB.cookies1.Where(x => x.DescriptionStatus.ToUpper() == "MODERATION"))
            {
                DynamicNode node = new DynamicNode();
                node.Title = ck.Name;
                node.Controller = "Cookie";
                node.Action = "CookieDetail";// +"/" + ck.Id;
                node.RouteValues.Add("id", ck.Id);

                returnValue.Add(node);
            }

            // Return
            return returnValue;
        }

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


    }


Note that these should be registered in Web.config, i.e. under pages tag  in webconfig .add the following:



  In    namespaces
     
        namespace="MvcSiteMapProvider.Web.Html" ;
 



No comments:

Post a Comment