MVC Sitemap Provider -- Maintaining URL parameters in breadcrumb trail
Asked Answered
L

2

9

I'm using the MvcSiteMapProvider from http://mvcsitemap.codeplex.com/ to create a breadcrumb trail for my project. 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?ID=101101) 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:

[SiteMapPreserveRouteData]
public ActionResult User()
{
  //code
}

Let me know if you need any more info from me.

Label answered 16/6, 2011 at 14:10 Comment(1)
I have the same challenge... Have you found a solution?Africanize
S
1

The way I did it, I took original mvc site map helper source for rendering breadcrumb, and changed it to handle parameters (although in my project we display only parameters for filtering and allow user to click on them to loose other filtering parameters, below is very naive implementation of node text, just an example how it can be done):

   private static string SiteMapText(this MvcSiteMapHtmlHelper helper, SiteMapNode node, string linkCssClass, IDictionary<string, object> htmlAttributes)
    {
        var extraAttributes = new StringBuilder();
        foreach (var attribute in htmlAttributes)
        {
            extraAttributes.Append(" " + attribute.Key + "=\"" + attribute.Value + "\"");
        }            

        string spanHtml;
        var paramDictionary = helper.HtmlHelper.ViewContext.RequestContext.HttpContext.Request.Params.ToDictionary();
        var queryParams = paramDictionary.Select(x => string.Format("{0}:{1}", x.Key, x.Value));

        // here you add request parameters
        var title = helper.HtmlHelper.Encode(string.Format("{0} ({1})", node.Title, string.Join(";", queryParams)));

        if (!string.IsNullOrEmpty(linkCssClass))
        {
            spanHtml = string.Format("<span><span class=\"{0}\"{1}>{2}</span>", linkCssClass, extraAttributes, title);
        }
        else
        {
            spanHtml = string.Format("<span><span{1}>{0}</span>", title, extraAttributes);
        }

        return spanHtml;
    }

In the same way you can adjust SiteMapLink methods, to include request parameters for current node.

Sorayasorb answered 17/1, 2012 at 7:23 Comment(0)
I
0

According to this thread on MVCSiteMapperProvider you can't use Query strings. It ignores query strings and only uses route data.

Query Strings and MVCSiteMapper

Have you thought about creating some new routes instead of using the query string?

Impotent answered 29/12, 2011 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.