Question

I am using MVC3, C#4, Razor, MvcSiteMapProvider V4.

I am trying to bind my MvcSiteMapNodes in my mvc.sitemap file explicitly to my routes in my global.asa. But it does not seem to work. No doubt I have omitted something.

my routes in global.asa:

  routes.MapRoute("Report", "Report/{ClientId}", new { controller = "Report", action = "Index", ClientId = UrlParameter.Optional });
  routes.MapRoute("Report_Section", "Report/{ClientId}/Section/{ReportId}", new { controller = "Section", action = "FilterByReport", ReportId = UrlParameter.Optional });

Part of my mvc.sitemap:

        <mvcSiteMapNode title="Reports" controller="Report" action="Index" preservedRouteParameters="ClientId" route="Report">
     <mvcSiteMapNode title="Sections" route="Report_Section">

So my 2 URLs are:

Reports Route:

Report/10

Reports_Sections Route:

Report/10/Section/100

I am not getting the recognition on the second route, and therefore the breadcrumb trail does not appear on the relevant page. I had hoped just by specifying the "route" attribute in mvc.sitemap, the mvcSiteMapProvider would bind the two.

Thoughts. Where have I gone wrong?

Many thanks

EDIT:

It seems that I need to specify the "controller" and "action" in "mvc.sitemap" and "route" on its own will not work.

         <mvcSiteMapNode title="Sections" controller="Section" action="FilterByReport"  route="Report_Section" preservedRouteParameters="ClientId,ReportId">
Was it helpful?

Solution

The route parameter is for narrowing the match to a specific route rather than trying to match all of the routes. However, just specifying the route is not enough to make it match. You also need to supply your action, controller, (optional) area, and (optional) action parameters.

Also, to make it appear to remember your breadcrumb trail, you need to add a node for each possible ClientID/ReportID combination to your sitemap. This can be achieved fairly easily by using a couple of DynamicNodeProviders to load your data from your database/datasource. The whole principle is based on matching a unique location within the map, and you need those IDs to make each match unique.

PreservedRouteParameters is typically used to make any ID match an action method parameter in CRUD operations. This gives you the ability to make your add/edit/delete pages match any ID without registering those IDs in the sitemap.

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