SiteMapProvider doesn't take QueryString into consideration
posted on Wednesday, November 23, 2005 by bobby @ 12:20 pm

Updated 11/27/2005 4:22:42 PM

I figured out a nifty workaround. Click here to view that post.


I was pretty stoked to find that ASP.NET 2.0 included a new control called SiteMap. This control will display a breadcrumb trail of links as you navigate through your site. By default, the data is loaded from a web.sitemap file that sits in the web app root. Although you can load the data from any source, this is the default and uses the XmlSiteMapProvider provider to populate the data.

This is an example of data found in the web.sitemap file:
Copy code to clipboard in IE or select code for Firefox
<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
    <siteMapNode url="~/default.aspx" title="Home">
        <siteMapNode url="~/hotels/list.aspx" title="Hotel Listings">
            <siteMapNode url="~/hotels/details.aspx" title="Hotel Information">
                <siteMapNode url="~/hotels/update.aspx" title="Update Hotel"/>
            </siteMapNode>
        </siteMapNode>
    </siteMapNode>
</siteMap>


This works fine if you aren't ever using querystring variables. But let's say when you get down to the details.aspx page, you tag on a HotelID variable to the querystring. Same thing happens when you go deeper into update.aspx.

Your breadcrumb will look like this:

Home > Hotel Listings > Hotel Information > Update Hotel

Given my current url is "update.aspx?HotelID=1234", I should have some way to propagate the current HotelID querystring variable to the other links, if they need it, which details.apx does.

I tried endlessly to get around this. I tried derriving my own class from XmlSiteProvider, but ASP.NET crashes as soon as you mess w/ the child nodes in the BuildSiteMap() method.

I tried binding an event handler to the SiteMap.SiteMapResolve event - but the Request collection is always unavailable at the time when this event is triggered.

I tried to modify the url's of the nodes showing on the screen dynamically, but supid SiteMapNode.Url property is Read-Only - ARGGG!!

It just amazes me that MS didn't see this coming & allow for it in the XML config. They could have added a "incorporate" attribute in the node that allowed you to specify a comma-delimited list of querystring variables that would be needed for that URL to work, like this:

Copy code to clipboard in IE or select code for Firefox
<siteMapNode url="~/hotels/details.aspx" title="Hotel Information" reliantOn="HotelID, UserID">


or give me a way to add in the querystring variables I want to pull from the current context like this:

Copy code to clipboard in IE or select code for Firefox
<siteMapNode url="~/hotels/details.aspx?HotelID={HotelID}" title="Hotel Information">


And the SiteMapProvider would be smart enough to replace the vars in braces w/ the variable from the current querystring.

I never thought getting something so simple to work would be so hard. If anyone has a solution, pony up.

CommentsComments
posted on Saturday, December 17, 2005  by Jazzy @ 8:24 PM

Hi Bobby

There is a way of doing this, although it took me a lot of hunting to find it.

Create a Global.asax file for your project if you don't already have one and add the following code:

Copy code to clipboard in IE or select code for Firefox
void Application_Start(object sender, EventArgs e) 
{
        SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(AppendQueryString);
}

SiteMapNode AppendQueryString(object o, SiteMapResolveEventArgs e)
    {
        if (SiteMap.CurrentNode != null)
        {
            SiteMapNode temp;
            temp = SiteMap.CurrentNode.Clone(true);
            Uri u = new Uri(e.Context.Request.Url.ToString());
            temp.Url += u.Query;
            if (temp.ParentNode != null)
            {
                temp.ParentNode.Url += u.Query;
            }
            return temp;
        }
        else
        {
            return null;
        }
    }


Hopefully, you'll find that does the trick!

Jazzy
posted on Sunday, December 18, 2005  by bobby @ 1:55 AM

Yeah, that' works - and that's what I tried the first time. But it didn't give me control over what querystring variables I wanted. It simply applies them all, all the time. I wanted to select certain variables for certain nodes.

The day after I made this post, I came up with a solution that solved my problem using a Custom SiteMapProvider, hence the link at the top of the post that says UPDATED.

Props on the workaround tho, I appreciate it. It's always nice to have options.

Cheers,
Bobby
posted on Wednesday, February 01, 2006  by maird @ 6:12 AM

ok. i'm having a heckuva time with this. i copied the code that Jazzy posted directly and it isn't working for me. i can step through the method and it looks like everything is working fine but when i look at the URL's the get output on my page, the querystring parameters aren't there. any ideas?

btw: i'm using a using a treeview with the sitemap as a datasource inside a masterpage. not sure if that makes any difference....
posted on Wednesday, February 01, 2006  by bobby @ 6:14 AM

Did you read the updated post? Link is at top.
posted on Wednesday, February 01, 2006  by maird @ 6:24 AM

yep. i tried that too. same effect. it looks like it should be rewriting the node but it doesn't work. i think it might have something to do with the treeview. i think i'm just going to handle the DataBound event of the treeview and rewrite the URL there...
posted on Monday, February 13, 2006  by Hexane! @ 10:28 PM

Ugh... I spent about 2 days going through the same exact hastles. I'm now building my bread crumb by hand, and setting it to a label. The shame. :(
posted on Monday, August 25, 2008  by Serge Struyk @ 5:58 AM

SiteMap.Provider.CurrentNode.ReadOnly = false;
posted on Friday, November 07, 2008  by JR @ 7:49 AM

Here's how to do it. It allows you to add in a property into the Web.sitemap entry to specify what querystring field/value to append to each link. Only thing is the farther down in the tree you get you need to supply the querystring values for all links that will appear before it.

http://weblogs.asp.net/jgaylord/archive/2008/06/04/adding-querystring-parameters-to-the-sitemapnode.aspx
posted on Thursday, November 27, 2008  by kiku chan @ 7:33 PM

thank
posted on Thursday, November 27, 2008  by Shuaib Khan @ 11:42 PM

Update ur site map xml file at run time
posted on Wednesday, March 11, 2009  by Hassan Voyeau @ 10:18 AM

this is what I did

http://haveworld.blogspot.com/2009/03/aspnet-include-querystring-parameters.html
posted on Monday, April 27, 2009  by Anonymous @ 4:33 PM

Hi,
The main problem is that the offered solution changes only the current node (viewed page).

If I have the following path:

a.aspx?a=1 -> b.aspx?b=1 and my current page is b.aspx?b=1 - I will not see the query string at a.aspx link in sitemap.

Does someone knows how that can be changed?
posted on Wednesday, July 01, 2009  by Adriano @ 2:58 AM

Hello Jazzy,
Thanks very much for the information above. It saved me a lot of time..
posted on Monday, November 02, 2009  by Dhanya @ 2:42 AM

Thanks Jazzy
posted on Wednesday, December 09, 2009  by chelland @ 9:45 AM

here is a simple solution to this problem that works:

if your link is - update.aspx?HotelID=1234
make a page with a name like Hotel1234.aspx
In the codebehind of the page put this in the page load event:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Server.Transfer("update.aspx?HotelID=1234")
End Sub

then use the hotel1234.aspx in your sitemap provider
posted on Wednesday, January 27, 2010  by Vini Bio @ 10:42 PM

Great post.It really contains valuable information.Thanks for sharing.
posted on Wednesday, March 03, 2010  by Anonymous @ 2:42 AM

Thanks man even though it doesnt solve entire issue. Its helping a lot ;) THanks a lot


New Post Notification

Search Posts

Recent Posts


About Meeself
People call me Bobby DeRosa
I live somewhere in San Diego, CA
MCSD, MCAD, MCP

This theme was adapted from fUnique by fahlstad        Icons by FamFamFam        XHTML 1.0 Strict; tuned for Mozilla-powered browsers

Admin Login Administrator Login
Invalid login attempts are logged.
  Username:
  Password: