Maintaining URL parameters in breadcrumb trail

Jun 14, 2011 at 3:29 PM

I have some URLs that need an ID to be passed to present information for the appropriate user, e.g. http://localhost:52306/Home/User?ID=101101

When I navigate further into the sitemap (e.g. http://localhost:52306/Home/User/Details) and try to use the breadcrumb link to take me back to the "User" page, the ID parameter is lost. I have tried adding SiteMapPreserveRouteData attributes to the action methods, but they don't seem to be doing anything. Is there an easy way to make sure this ID information is preserved? I thought the SiteMapPreserveRouteDataAttribute was supposed to do this, so is there something I am doing wrong with the attribute? My method would look like this:

public ActionResult User()

Jun 29, 2011 at 11:27 AM

Yes, for the current node. Not for the parent node. You can add your own attribute if you'ld like, adding route values to the parent node (which I'll add to the codebase):

    public class SiteMapPreserveRouteDataForParentAttribute : System.Web.Mvc.ActionFilterAttribute
        /// <summary>
        /// Ensures all routedata elements are included on the node whenever the mvc action is invoked.
        /// This allows the MVC site map to have route values preserved for breadcrumb trails.
        /// </summary>
        /// <param name="filterContext">The current filter context.</param>
        public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
            var node = SiteMap.CurrentNode.ParentNode as MvcSiteMapNode;
            if (node != null)
                foreach (var routeitem in filterContext.RouteData.Values)
                    node.RouteValues[routeitem.Key] = routeitem.Value;
Jun 30, 2011 at 3:44 PM

I've added this method to the source code and I still can't seem to get it working. If I do not use dynamic nodes, all the breadcrumbs display but the ID is not preserved. If I use a dynamic node for the "User" page and add the route values I want in the dynamic node provider, only the current and the parent node displays once I navigate to the "User" page's child node (and the breadcrumb link still doesn't preserve the route values). Here is the other discussion I started about the problem that provides more details:

In my controller, I have the SiteMapPreserveRouteData attribute on the User controller, and the SiteMapPreserveRouteDataForParent attribute on its child controller.

Why doesn't the site map path display all the breadcrumbs? Why isn't the ID preserved in the breadcrumb trail?