简体   繁体   中英

Asp.Net Mvc 4 SiteMap

I want to add SiteMap in My Project But I still Got Error

Not all configured nodes could be paired with a parent node. Check your parent keys to ensure that a node with a corresponding key exists in the SiteMap. Note that the match is case sensitive.

Here is My Site Map

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
            xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">

  <mvcSiteMapNode title="Home" controller="Home" action="Index" key="Home">
    <mvcSiteMapNode title="Quiz" controller="Quiz" action="Index" key="Quiz" dynamicNodeProvider="SiteMapExample.Models.DynamicNodeCategory,SiteMapExample" >

    </mvcSiteMapNode>
  </mvcSiteMapNode>

</mvcSiteMap>

and here is My Model which Generate the Dynamic Node For SiteMap

 public class DynamicNodeCategory : DynamicNodeProviderBase
    {
        public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
        {
            string[]category={".Net","Java","C"};
            for (int i = 0; i < 3; i++)
            {
                DynamicNode dNode = new DynamicNode();
                dNode.ParentKey = "Quiz";
                dNode.Title = category[i];              
                dNode.Action = "Test";
                dNode.Controller = "Quiz";
                dNode.Key = dNode.Title;
                dNode.RouteValues.Add("category", dNode.Title);
                yield return dNode;
            }
        }
    }

and i am using Sitemap here

@Html.MvcSiteMap().SiteMapPath()

The mvcSiteMapNode element that defines the DynamicNodeProvider is not added to the sitemap. It is just a definition node. The data that is defined in the definition node becomes the default values for each dynamic node.

Therefore, there is no actual node with the key "Quiz" in your configuration. To fix that, add another mvcSiteMapNode to define your key with. This extra node will be in your sitemap.

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
            xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">

  <mvcSiteMapNode title="Home" controller="Home" action="Index" key="Home">
    <mvcSiteMapNode title="Quiz" controller="Quiz" action="Index" key="Quiz">
        <!-- dynamic node definition -->
        <mvcSiteMapNode controller="Quiz" action="Test" dynamicNodeProvider="SiteMapExample.Models.DynamicNodeCategory,SiteMapExample" />
    </mvcSiteMapNode>
  </mvcSiteMapNode>

</mvcSiteMap>

If you do not want an extra node in your sitemap below the home page, rather than adding another node specify "Home" as your parent key in your dynamic nodes.

Also, since your controller and action are already being provided in the definition node, you don't have to provide them again in the dynamic node provider. You could either do it as shown below, or simply not provide the controller and action on the definition node and uncomment those 2 lines here - that will allow you to define everything in one place.

public class DynamicNodeCategory : DynamicNodeProviderBase
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        string[]category={".Net","Java","C"};
        for (int i = 0; i < 3; i++)
        {
            DynamicNode dNode = new DynamicNode();
            dNode.ParentKey = "Quiz";
            dNode.Title = category[i];    

            // Technically not needed because they are being provided by the definition node
            // dNode.Action = "Test";
            // dNode.Controller = "Quiz";

            dNode.Key = dNode.Title;
            dNode.RouteValues.Add("category", dNode.Title);
            yield return dNode;
        }
    }
}

Note that it doesn't matter where the dynamic node definition is physically placed in the XML file - it always uses the parentKey to determine where to attach the nodes.

Update

If you would rather use the [MvcSiteMapNode] attribute, you can do the same thing as you would in XML. The node that your dynamic node provider is declared on is still a definition node that is not added to the sitemap, but is used to define the default values for each dynamic node.

public class QuizController : Controller
{
    [MvcSiteMapNode(Title = "Quiz", Key = "Quiz")]
    public ActionResult Index()
    {
        // Implemenatation here
        return View();
    }

    [MvcSiteMapNode(DynamicNodeProvider = "SiteMapExample.Models.DynamicNodeCategory,SiteMapExample")]
    public ActionResult Test()
    {
        // Implemenatation here
        return View();
    }
}

The only difference is that the controller and action are detected automatically so you don't have to specify them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM