If you’re familiar with the System.Uri class you know that it has a couple of ways of becoming a string for purposes of communication: its AbsoluteUri property and its ToString() method. What I didn’t find out until very recently was the significant difference between the two. Since the difference led to functional bugs in production code, I thought I’d share the knowledge to help you avoid the same fate.
Given a Uri instance constructed like this:
UriBuilder builder = new UriBuilder("http://somehost/somepath"); builder.Query = "somekey=" + HttpUtility.UrlEncode("some+value"); Uri someUri = builder.Uri;
Uri.ToString() will return the human-readable URL:
Notice the plus sign in "some+value", the way we put it originally, before HttpUtility.UrlEncode encoded it.
Uri.AbsoluteUri on the other hand will return the encoded form as HttpUtility.UrlEncode returned it:
Notice this time how the plus sign has been (is still) encoded.
So why is this important? Well if you make a request with some+value, then the web server will receive "some value". But if you make a request with some%2bvalue, then the web server will receive "some+value", which is what you originally intended. Plus signs aren’t the only symbol that can be misinterpreted, of course.
What lesson can we derive from this? Unless you’re displaying a URL for diagnostic purposes (and perhaps even then) always use Uri.AbsoluteUri instead of Uri.ToString().
Incidentally, UriBuilder.ToString() does not decode the URL, so UriBuilder.ToString() is safe to use. It’s just Uri.ToString() that you should avoid.