Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep url encoded while using URI class in .NET 3.5

Tags:

c#

.net

uri

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?

like image 264
user2018364 Avatar asked Nov 13 '22 12:11

user2018364


1 Answers

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.

like image 105
Arpit Jain Avatar answered Nov 15 '22 04:11

Arpit Jain