How can i change the parent breadcrumb based on the page i am called from or how can i add a child node in MvcSiteMapNode

StackOverflow https://stackoverflow.com/questions/22795935

  •  25-06-2023
  •  | 
  •  

Question

I have a Person Create page which is called from two different pages. I want to show the parent node of this Create page based of the page i am called from.

<mvcSiteMapNode title="Create" controller="Person" action="Create"   />

I want the above node to be inside the below parent node based from where it is called from.

<mvcSiteMapNode title="All People" controller="Person" action="AllPeople"  key="AllPeople"  >
 </mvcSiteMapNode>

<mvcSiteMapNode title="People" controller="Person" action="Index"   >          

 </mvcSiteMapNode>

Thanks in advance

Was it helpful?

Solution

To accomplish this, you need to make 2 separate nodes and ensure each one has a unique URL. MvcSiteMapProvider requires that each node has a unique routing signature in order to function. Each node would be placed as a child of the respective category.

You can make a unique URL by adding a value to the query string or by changing your routes so you have a completely different URL in each case. Once you have a unique URL in each case and have configured the route information to create each URL in your nodes, the breadcrumbs will work as expected.

<mvcSiteMapNode title="All People" controller="Person" action="AllPeople" key="AllPeople">
    <mvcSiteMapNode title="Create" controller="Person" action="Create" category="AllPeople" />
</mvcSiteMapNode>

<mvcSiteMapNode title="People" controller="Person" action="Index">          
    <mvcSiteMapNode title="Create" controller="Person" action="Create" />
</mvcSiteMapNode>

Results in:

All People > Create (URL is /Person/Create?category=AllPeople)
People > Create (URL is /Person/Create)

However, there is one more thing you need to do if your duplicate page is Internet facing - add a canonical tag. This will tell search engines that you intended to put the same content on 2 different URLs. You can decide which specific URL you want to be shown in the index and then add the canonicalUrl with that URL or canonicalKey with the key of that node to the "duplicate" nodes.

<mvcSiteMapNode title="All People" controller="Person" action="AllPeople" key="AllPeople">
    <mvcSiteMapNode title="Create" controller="Person" action="Create" category="AllPeople" canonicalKey="CreatePerson" />
</mvcSiteMapNode>

<mvcSiteMapNode title="People" controller="Person" action="Index">          
    <mvcSiteMapNode title="Create" controller="Person" action="Create" key="CreatePerson" />
</mvcSiteMapNode>

Then add a @Html.MvcSiteMap().CanonicalTag() HTML helper to the <head></head> section (usually in a layout page), and MvcSiteMapProvider will automatically generate the Canonical tag when necessary.

<head>
    <meta charset="utf-8" />
    <title>@Html.MvcSiteMap().SiteMapTitle() - My ASP.NET MVC Application</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Html.MvcSiteMap().CanonicalTag()
    @Html.MvcSiteMap().MetaRobotsTag()
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>

More information:

http://www.shiningtreasures.com/post/2013/08/10/mvcsitemapprovider-4-seo-features#canonical-tag https://github.com/maartenba/MvcSiteMapProvider/wiki/Multiple-Navigation-Paths-to-a-Single-Page http://www.mattcutts.com/blog/canonical-link-tag/

Be sure to check out the code download in the first post for a working demo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top