1

I am using MvcSiteMapProvider to implement SiteMap in a MVC project for the first time.My xml is(Mvc.sitemap):

  <mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="Blog" controller="Blog" action="Index">
      <mvcSiteMapNode title="Entry" controller="Blog" action="Entry"/><!--Not working-->
    </mvcSiteMapNode>
  </mvcSiteMapNode>

I am more than sure the xml is just fine.

SiteMap for (Home,Index) and (Blog,Index) work. But not (Blog,Entry); The model count is 0 of SiteMapHelperModel.cshtml:

@foreach (var node in Model)
 { 
    @Html.DisplayFor(m => node);

    if (node != Model.Last()) {
        <text> &gt; </text>
    }
}

What is the reason that the node count is zero, when the url being generated is correct too?

user3286962
  • 147
  • 2
  • 10

1 Answers1

0

I would venture to guess that your blog entry has some sort of identifier ("id") that is causing the node not to match. Custom parameters must always be manually dealt with. There are 2 ways it can be done.

Option 1

By default, you are expected to create 1 node per custom parameter combination, such as:

<mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="Blog" controller="Blog" action="Index">
        <mvcSiteMapNode title="Blog Entry 1" controller="Blog" action="Entry" id="1"/>
        <mvcSiteMapNode title="Blog Entry 2" controller="Blog" action="Entry" id="2"/>
        <mvcSiteMapNode title="Blog Entry 3" controller="Blog" action="Entry" id="3"/>
    </mvcSiteMapNode>
</mvcSiteMapNode>

If your data is based on an external datasource, this is best done using a dynamic node provider or by implementing ISiteMapNodeProvider.

This is the recommended way if you want your nodes to be visible in the menu and/or /sitemap.xml endpoint so your pages can be submitted to search engines. Just try to keep the total number of nodes below 10,000.

Option 2

You can force a single node to match any value for "id" by adding it as a preservedRouteParameter.

<mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="Blog" controller="Blog" action="Index">
        <mvcSiteMapNode title="Entry" controller="Blog" action="Entry" preservedRouteParameters="id"/>
    </mvcSiteMapNode>
</mvcSiteMapNode>

You typically need to fix the title by using the SiteMapTitleAttribute. Also, it is not possible for all of the nodes to show up in the Menu HTML helper, SiteMap HTML helper, or /sitemap.xml endpoint using this approach. You get a breadcrumb trail, but that is about it. For that reason, it is best to hide this node from the other controls using the FilteredSiteMapNodeVisibilityProvider.

This approach is ideal for CRUD operations, to ignore "session" parameters, or for situations where you would have so many nodes in the SiteMap that it causes performance to degrade.

These 2 options can be combined on the same node (but different parameters).

For an in depth look at these options with downloadable demos, see How to Make MvcSiteMapProvider Remember a User's Position.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • The generated url is dynamic like "http://localhost:11517/Blog/2014/7/11/news", "http://localhost:11517/Blog/2009/10/19/my-first-post" for "Blog,Entry page";(I have used routing for this). I want all pages having similar structure(i.e. http://localhost:11517/Blog/{date-yyyy/mm/dd}/{blogName}) to be identified as "Blog,Entry" page as am implementing breadcrumb too. Any more suggestions? It would be a great help – user3286962 Jul 28 '14 at 13:25
  • First, think about how you will make your application look up a blog entry based on the information in the URL. Next, configure your route to provide the information to your application. Third, get your controller action to work. Once you have done this, provide the route parameters you used in your route to MvcSiteMapProvider. If you post your route information, I can help with that. If you implement custom route code inherit RouteBase instead of RouteHandler because RouteHandler cannot perform 2-way routing. Here is some food for thought: http://stackoverflow.com/questions/19611863 – NightOwl888 Jul 29 '14 at 04:03