title: Creating child nodes for a DynamicNode in MvcSiteMapProvider that have dynamic parameters
body: <p>I am using MvcSiteMapProvider 2.2.1 (http://mvcsitemap.codeplex.com), and am having a problem with creating children under a dynamic node (using a dynamicNodeProvider) when those children have a dynamic parameter (id). </p>
  
  <p>I am losing breadcrumbs for the following route:</p>
  
  <p><strong>Stores/5/Products/Edit/23</strong></p>
  
  <p>where the url pattern is:</p>
  
  <p><strong>Stores/{storeID}/{controller}/{action}/{id}</strong></p>
  
  <p>It works fine when the ID is left out (ie the "New" action). But when the ID is specified, it doesn't match the route, and my breadcrumbs (using the SiteMapPath helper) is blank. </p>
  
  <p><strong>My Sitemap:</strong> (abreviated)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-2.0"&gt;
      &lt;mvcSiteMapNode title="Home" controller="Dashboard" action="Index" changeFrequency="Always" updatePriority="Normal"&gt;
          &lt;mvcSiteMapNode title="My Account" controller="Account" action="Index" key="Account" /&gt;
          &lt;mvcSiteMapNode title="My Stores" area="Stores" controller="Home" action="Index" visibilityProvider="ControlPanel.Areas.Stores.StoreAreaVisibilityProvider, ControlPanel"  &gt;
              &lt;mvcSiteMapNode title="Store" action="Index" dynamicNodeProvider="ControlPanel.Areas.Stores.StoreAreaNodeProvider, ControlPanel" /&gt;
          &lt;/mvcSiteMapNode&gt;
      &lt;/mvcSiteMapNode&gt;
  &lt;/mvcSiteMap&gt;
  </code></pre>
  
  <p><strong>Area Registration:</strong></p>
  
  <pre><code>public override void RegisterArea(AreaRegistrationContext context)
  {
          context.MapRoute(
              "Store_Index",
              "Stores",
              new { action = "Index", controller = "Home" },
              new string[] { "ControlPanel.Areas.Stores.Controllers" }
              );
  
          context.MapRoute(
              "Store_default",
              "Stores/{storeID}/{controller}/{action}/{id}",
              new { action = "Index", controller = "Manage", id = UrlParameter.Optional },
              new { storeID = @"\d+" },
              new string[] { "ControlPanel.Areas.Stores.Controllers" }
          );
      }
  </code></pre>
  
  <p><strong>First Attempt:</strong></p>
  
  <p>The first thing I tried was to create the child nodes right in the sitemap xml as children of the dynamic node. This didn't work at all, and these ended up being children of "Home". I would put a ParentKey attribute in there, except these will be repeated per store and thus there will be multiple parentkeys</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-2.0"&gt;
    &lt;mvcSiteMapNode title="Home" controller="Dashboard" action="Index" changeFrequency="Always" updatePriority="Normal"&gt;
      &lt;mvcSiteMapNode title="My Account" controller="Account" action="Index" key="Account" /&gt;
      &lt;mvcSiteMapNode title="My Stores" area="Stores" controller="Home" action="Index" visibilityProvider="ControlPanel.Areas.Stores.StoreAreaVisibilityProvider, ControlPanel"  &gt;
        &lt;mvcSiteMapNode title="Store" action="Index" dynamicNodeProvider="ControlPanel.Areas.Stores.StoreAreaNodeProvider, ControlPanel"&gt;
          &lt;mvcSiteMapNode title="Products" area="Stores" controller="Products" action="Index"&gt;
            &lt;mvcSiteMapNode title="Edit" area="Stores" controller="Products" action="Edit"/&gt;
            &lt;mvcSiteMapNode title="New" area="Stores" controller="Products" action="Edit"/&gt;
          &lt;/mvcSiteMapNode&gt;
        &lt;/mvcSiteMapNode&gt;
      &lt;/mvcSiteMapNode&gt;
    &lt;/mvcSiteMapNode&gt;
  &lt;/mvcSiteMap&gt;
  </code></pre>
  
  <p><strong>Second Attempt:</strong></p>
  
  <p>It seemed the next option was to just add the child nodes right in my DynamicNodeProvider. This worked much better except for the nodes that had dynamic parameters</p>
  
  <p>(the below is modified for ease of explanation...)</p>
  
  <pre><code>public class StoreAreaNodeProvider : IDynamicNodeProvider
  {
      public IEnumerable&lt;DynamicNode&gt; GetDynamicNodeCollection()
      {
          var nodes = new List&lt;DynamicNode&gt;();
  
          foreach (var store in repo.GetStores())
          {
              DynamicNode node = new DynamicNode();
              node.Title = store.Name;
              node.Area = "Stores";
              node.Controller = "Manage";
              node.Action = "Index";
              node.RouteValues.Add("storeID", store.StoreID);
              node.Key = "Store_" + store.StoreID.ToString();
  
              nodes.Add(node);
  
              //Child of node
              DynamicNode productsNode = new DynamicNode();
              productsNode.Title = "Products";
              productsNode.Area = "Stores";
              productsNode.Controller = "Products";
              productsNode.Action = "Index";
              productsNode.RouteValues.Add("storeID", store.StoreID);
              productsNode.ParentKey = String.Format("Store_{0}", store.StoreID.ToString());
              productsNode.Key = String.Format("Store_{0}_Products", store.StoreID.ToString());
  
              nodes.Add(productsNode);
  
              //child of productsNode
              DynamicNode editNode = new DynamicNode();
              editNode.Title = "Edit";
              editNode.Area = "Stores";
              editNode.Action = "Edit";
              editNode.Controller = "Products";
              editNode.RouteValues.Add("storeID", store.StoreID);
              //I can't add the RouteValue "ID" here because it is dynamic
              //I would have do loop through every product for this store with
              //another dynamic node provider, but that seems terribly inefficient and stupid
              editNode.ParentKey = String.Format("Store_{0}_Products", store.StoreID.ToString());
              editNode.Attributes.Add("visibility", "SiteMapPathHelper,!*");
  
              nodes.Add(editNode);
          }
  
          return nodes;
      }
  }
  </code></pre>
  
  <p><strong>In Summary</strong></p>
  
  <p>Does Work: <em>Stores/5/Products/New</em><br>
  Doesn't Work: <em>Stores/5/Products/Edit/23</em><br>
  For Url Pattern: <em>Stores/{storeID}/{controller}/{action}/{id}</em>  </p>
  
  <p>What I would like to be able to do:</p>
  
  <pre><code>editNode.Attributes.Add("isDynamic", "true");
  editNode.Attributes.Add("dynamicParameters", "id");
  </code></pre>
  
  <p>How can I mimick the old MvcSiteMapProvider's dynamicParameters attribute on a node that is a child of a dynamicNode? Basically I need it to ignore the "id" route value when matching routes.</p>
  
  <p>Hopefully I explained that properly, and didn't overwhelm you with information. Thanks!</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here is the solution that worked for me based on Jakub's answer.</p>
  
  <p>In MvcSiteMapProvider 2.x, you can make your own implementation of ISiteMapNodeUrlResolver instead of having to modify the source. So I basically added back in the ability to have the dynamicParameters attribute</p>
  
  <p><strong>Class:</strong></p>
  
  <pre><code>namespace ControlPanel
  {
      public class CustomSiteMapNodeUrlResolver : ISiteMapNodeUrlResolver
      {
          public virtual string ResolveUrl(MvcSiteMapNode mvcSiteMapNode, string area, string controller, string action, IDictionary&lt;string, object&gt; routeValues)
          {
              RequestContext ctx;
              if (HttpContext.Current.Handler is MvcHandler)
                  ctx = ((MvcHandler)HttpContext.Current.Handler).RequestContext;
              else
                  ctx = new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData());
  
              //Begin Added Code
              if (mvcSiteMapNode["dynamicParameters"] != null)
              {
                  foreach (var item in mvcSiteMapNode["dynamicParameters"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                  {
                      var dp = item.Trim();
                      routeValues[da] = ctx.RouteData.Values[dp];
                  }
              }
              //End Added Code
  
              return new UrlHelper(ctx).Action(action, controller, new RouteValueDictionary(routeValues));
          }
      }
  }
  </code></pre>
  
  <p><strong>Web.config:</strong></p>
  
  <pre><code>&lt;siteMap defaultProvider="MvcSiteMapProvider" enabled="true"&gt;
    &lt;providers&gt;
      &lt;clear/&gt;
      &lt;add name="MvcSiteMapProvider"
           type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider"
           siteMapFile="~/Mvc.Sitemap"
           securityTrimmingEnabled="true"
           attributesToIgnore="visibility,dynamicParameters"
           scanAssembliesForSiteMapNodes="true" 
           siteMapNodeUrlResolver="ControlPanel.CustomSiteMapNodeUrlResolver, ControlPanel"
           siteMapNodeVisibilityProvider="MvcSiteMapProvider.FilteredSiteMapNodeVisibilityProvider, MvcSiteMapProvider" /&gt;
    &lt;/providers&gt;
  &lt;/siteMap&gt;
  </code></pre>
  
  <p><strong>Dynamic Node Provider:</strong></p>
  
  <pre><code>DynamicNode node = new DynamicNode();
  node.Attributes.Add("dynamicParameters", "id");
  </code></pre>