Nested dynamicNodeProvider: Solution or Workaround?

Feb 27, 2011 at 9:57 PM

I'm posting this more as a benefit to others that might have the same issue - It took me a bit of headbanging and randomness to figure it out

Using 3.0.0 version of MvcSiteMapProvider, I have a sitemap with nested dynamic nodes, similar to the example app, as such:

 

Mvc.Sitemap:

 

<mvcSiteMapNode title="Category.Name" controller="Products" action="Category" route="CategoryUrl" dynamicNodeProvider="Project.ProductCategoryDynamicNodeProvider, Assembly">
    <mvcSiteMapNode title="Product.Name" controller="Products" action="Details" route="ProductUrl" dynamicNodeProvider="Project.ProductDynamicNodeProvider, Assembly"/>
</mvcSiteMapNode>

 

 

Routing in Global.asax:

 

routes.MapRoute(
  "CategoryUrl",
  "Products/{categoryUrl}", 
  new { controller = "Products", action = "Category" } 
);

routes.MapRoute(
  "ProductUrl",
  "Products/{categoryUrl}/{productUrl}",
  new { controller = "Products", action = "Details"} 
);



ProductCategoryDynamicNodeProvider.cs:

 

public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
    var model = new ProductsModel();

    // Create a node for each category 
    foreach (var category in model.Categories)
    {
        DynamicNode node = new DynamicNode("ProductCategory_" + category.Id, category.Name);
        node.RouteValues.Add("categoryUrl", category.Url);

        yield return node;
    }
}

 

 

ProductDynamicNodeProvider.cs:

public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
    var model = new ProductsModel();

    // Create a node for each product 
    foreach (var product in model.Products)
    {
        DynamicNode node = new DynamicNode();
        node.Title = product.Name;
        node.ParentKey = "ProductCategory_" + product.Category.Id;
        node.RouteValues.Add("productUrl", product.Url);
        node.RouteValues.Add("categoryUrl", product.Category.Url);

        yield return node;
    }
}

If I used just one of the dynamic nodes - either one, category or products -  without a subordinate node it would work fine; the model, custom node providers, and XML are valid.  But, as soon as I added the Products node under the category node, I would get an exception while building the sitemap:

Could not resolve URL for sitemap node

 

After much head-to-desk I finally tried something on a whim... explicitly setting the Key for the subordinate dynamic node instead of relying on auto-generation, e.g.:

DynamicNode node = new DynamicNode("Product_" + product.Id, product.Name);

 

And it worked!

Nothing in the example app led me to this conclusion nor was it clear why explicitly setting a node Key for the subordinate node would make the difference.  

So, I guess if I have a question, it is:  Is this a bug or a feature?

Coordinator
Mar 25, 2011 at 8:50 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Mar 25, 2011 at 9:00 PM

Can you create a small repro? If not, can you post a stacktrace?