views:

802

answers:

4

I have a query string parameter value that contains an ampersand. For example, a valid value for the parameter may be:

a & b

When I generate the URL that contains the parameter, I'm using System.Web.HTTPUtility.UrlEncode() to make each element URL-friendly. It's (correctly) giving me a URL like:

http://example.com/foo?bar=a+%26b

The problem is that ASP.NET's Request object is interpreting the (encoded) ampersand as a Query String parameter delimiter, and is thus splitting my value into 2 parts (the first has "bar" as the parameter name; the second has a null name).

It appears that ASP.NET is URL-decoding the URL first and then using that when parsing the query string.

What's the best way to work around this?


UPDATE: The problem hinges on URLRewriter (a third-party plugin) and not ASP.NET itself. I've changed the title to reflect this, but I'll leave the rest of the question text as-is until I find out more about the problem.

A: 

Switching to UrlRewritingNet.UrlRewrite did not help, as it apparently has the same bug. I'm thinking it might have something to do with ASP.NET after all.

Craig Walker
+1  A: 

man, i am with you in the same boat, i have spent like hours and hours trying to figure out what is the problem, and as you said it is a bug in both, as normal links that contain weird characters or UTF-8 code characters are parsed fine by asp.net.

i think we have to switch to MVC.routing

Update: man you wont believe it, i have found the problem it is so strange, it is with IIS, try to launch your page from visual studio Dev server and Unicode characters will be parsed just fine, but if you launch the page from IIS 7 it will give you the ???? characters.

hope some body will shade some light here

+1  A: 

I would have thought that %26 and '&' mean exactly the same thing to the web server, so its the expected behavior. Urlencode is for encoding URLs, not encoding query strings.

... hang on ... Try searching for abc&def in google, you'll get:

http://www.google.com.au/search?q=abc%26def

So your query string is correct, %26 is a literal ampersand. Hmm you're right, sounds like a bug. How do you go with an & instead of the %26 ?

Interesting reading:

http://www.stylusstudio.com/xsllist/200104/post11060.html

Chris
A: 

I think URLRewriter has a problem with nameless parameters (null name).
I had a similar problem. When I gave my nameless parameter a (dummy) name, everything worked as expected.

Yahoo Serious