Skip to content

URLs UrlDecoded

September 17, 2012

We had an issue that some urls got UrlDecoded automatically without any apparent reason. We had to find out that some .NET methods implicitly UrlDecode the URLs even if you don’t want that.

The following methods surely decode the URLs, but there might be more:

HttpUtility.ParseQueryString(querystring)

This method returns a NameValueCollection that contains the QueryString parameters UrlDecoded.
If you want to use this method but don’t want the parameters to be decoded, you can use your own parsing algorithm. You can find one here:
http://stackoverflow.com/questions/68624/how-to-parse-a-query-string-into-a-namevaluecollection-in-net

Uri.ToString

You may expect that the result of new Uri(“url”).ToString() will equal to “url” but that is often not the case

The MSDN documentation and the sample code
also describe that the result of Uri.ToString is not the same as the original string. If you need the original, use the OriginalString property.

Uri uriAddress = new Uri("HTTP://www.Contoso.com:80/thick%20and%20thin.ht");

// The following outputs "http ://www.contoso.com/thick and thin.htm".
Console.WriteLine(uriAddress.ToString()); 

Probably you should not use this method in your code, unless you are logging or debugging. Use the OriginalString or AbsoluteUri properties instead.

Advertisements

From → .NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: