Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Literal ampersands in System.Uri query string

I'm working on a client app that uses a restful service to look up companies by name. It's important that I'm able to include literal ampersands in my queries since this character is quite common in company names.

However whenever I pass %26 (the URI escaped ampersand character) to System.Uri, it converts it back to a regular ampersand character! On closer inspection, the only two characters that aren't converted back are hash (%23) and percent (%25).

Lets say I want to search for a company named "Pierce & Pierce":

var endPoint = "http://localhost/companies?where=Name eq '{0}'";
var name = "Pierce & Pierce";
Console.WriteLine(new Uri(string.Format(endPoint, name)));
Console.WriteLine(new Uri(string.Format(endPoint, name.Replace("&", "%26"))));
Console.WriteLine(new Uri(string.Format(endPoint, Uri.EscapeUriString(name))));
Console.WriteLine(new Uri(string.Format(endPoint, Uri.EscapeDataString(name))));

All four of the above combinations return:

http://localhost/companies?where=Name eq 'Pierce & Pierce'

This causes errors on the server side since the ampersand is (correctly) interpreted as a query arg delimiter. What I really need it to return is the original string:

http://localhost/companies?where=Name eq 'Pierce %26 Pierce'

How can I work around this behavior without discarding System.Uri entirely? I can't replace all ampersands with %26 at the last moment because there will usually be multiple query args involved and I don't want to destroy their delimiters.

Note: A similar problem was discussed in this question but I'm specifically referring to System.Uri.

like image 662
Nathan Baulch Avatar asked May 20 '10 09:05

Nathan Baulch


1 Answers

It's not only the ampersand that is incorrect in the URL. A valid URL can not contain spaces.

The EscapeDataString method works just fine to encode the string properly, and you should encode the entire value, not just the name:

Uri.EscapeDataString("Name eq 'Pierce & Pierce'")

Result:

Name%20eq%20'Pierce%20%26%20Pierce'

When you create an Uri using this string, it will be correct. To see the URL you can use the AbsoluteUri property. If you just convert the Uri to a string (which calls the ToString method) the URL will be unescaped and thus appear incorrect.

like image 157
Guffa Avatar answered Nov 15 '22 10:11

Guffa