user2018364
user2018364

Reputation: 51

Keep url encoded while using URI class in .NET 3.5

I am using .NET 3.5.

Both solutions which are described here (the property "genericUriParserOptions" in config file and constructor parameter "dontEscape") don't work for .NET 3.5.

I want that URI constructor doesn't escape (means I want to have escaped URL parts) anything. Now I can't use configuration file with

genericUriParserOptions="DontUnescapePathDotsAndSlashes"

bacause this property is only available for .NET 4.0. But I can't also use "dontEscape" parameter in URI constructor because the constructor is obsolete in .NET 3.5 and is always false.

How I can create an URI with escaped string in .NET 3.5?

Upvotes: 5

Views: 1654

Answers (1)

Arpit Jain
Arpit Jain

Reputation: 455

You should encode only the user name or other part of the URL that could be invalid. URL encoding a URL can lead to problems since something like this:

string url = HttpUtility.UrlEncode("http://www.google.com/search?q=Example");

Will yield

http%3a%2f%2fwww.google.com%2fsearch%3fq%3dExample

This is obviously not going to work well. Instead, you should encode ONLY the value of the key/value pair in the query string, like this:

string url = "http://www.google.com/search?q=" + HttpUtility.UrlEncode("Example");

Thanks.

Upvotes: 1

Related Questions