MvcSiteMap: Dynamic node provider not reached

2019-01-28 05:04发布

问题:

I created a project that the nodes are defined using attributes, and I set it in the web.config to scan for attributes, and it works fine.
I don't use an XML file at all.

Now I want to add a dynamic node provider, how do I do it?
Is there a way to do it without the XML (.sitemap) file?
I need to make sure it's under the root, which has been set in code using MvcSiteMapNodeAttribute attribute.

I've read the documentation and I can't really figure out where to place this line:

<mvcSiteMapNode
  title="Details" action="Details"              
  dynamicNodeProvider="Project.StoreDetailsDynamicNodeProvider, Prject" />

What action is it supposed to point to? Additionally as said above, the root element is defined using attributes, so my question is if there is a way to avoid XML, or alternatively what's the efficient way to declare the XML (the less the better) to include my dynamic provider.

Update

I've tried the following and the node provider still isn't reached (From HomeController.cs).

[MvcSiteMapNode(Title = "Home", Key = HomeMenuKey,
  DynamicNodeProvider = "Project.Namespace.NodeProvider, Assembly")]
public ActionResult Index()
{
  return View();
}

回答1:

Can you define it in the controller method attributes (and not use XML at all)?

For example:

[MvcSiteMapNode(Title="Details", 
  DynamicNodeProvider = "Project.StoreDetailsDynamicNodeProvider, Project")]
public ActionResult Index()
{
    return View();
}


回答2:

  1. Seems that the dynamicNodeProvider attribute is ignored in the root node, also when it's defined in attributes.
    So the only way to add a dynamic node provider under the root, is either by specifying it on a dummy action etc. or using XML.
  2. An interesting note: the actual difference between defining in XML and attributes is that if it's defined in attributes, it (i.e. the gen. menu items) will be last in the menu, whereas when defined in XML it will be right after the root item (I guess that would be Home), Note that this is still controllable via the Order property in the attributes.
  3. In my Web.Config, I left the siteMapFile empty, relying in what it said in the wiki page, that the default value is ~/Web.sitemap, in fact this is false (I've already corrected that in the updated wiki).
    I don't think this behavior should be like this, I do think the MvcSiteMap engine should scan for dynamic node providers just as it scans for dynamic node attributes (here is the issue I posted on site).