I am using MVCSiteMapProvider 4.6.7 in an ASP.NET MVC 5 project. I have this in the mvc.sitemap
file:
<mvcSiteMapNode title="UserManager" controller="User" action="Index">
<mvcSiteMapNode title="UserAdd" controller="User" action="Create"/>
<mvcSiteMapNode title="UserEdit" controller="User" action="Edit"
preservedRouteParameters="id"
key="User_Edit"/>
<mvcSiteMapNode title="UserView" controller="User" action="Details"/>
</mvcSiteMapNode>
I am showing a Site Map Path like this:
<div id="breadcrumbNav">
@Html.MvcSiteMap().SiteMapPath()
</div>
When I navigate to http://siteurl/User/Edit/2
, the site map path is displayed, but when I navigate to http://siteurl/User/Edit?id=2
, it is not.
Removing preservedRouteParameters="id"
will reverse the situation ( site map displayed with .../Edit?id=2
, but not with .../Edit/2
)
Is there a way to make them both work?
This was due to a bug, which has now been fixed in v4.6.10 (thanks for the report).
The problem is that "id" is specified as an optional route value, in which case MVC automatically generates a route value with "id/null" when "id" is not provided as a route value in the URL. Since route values take precedence over query string values when finding the "current" node, the route value was not being overwritten in this case. An exception was added for this specific case in v4.6.10 to overwrite route values that are empty so the match will be done with the query string value instead of the empty route value.
After you upgrade, using preservedRouteParameters="id"
will match in both cases, and the breadcrumb will be shown.