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?

Community
  • 1
  • 1
  • 1
    Is the `Uri.OriginalString` property of any help to you? Basically, let the constructor do what it does, but you work with the original value that you passed to the constructor. – Jim Mischel Jan 29 '13 at 18:55
  • Hi Jim, thanks for suggestion. I need URI in order to create http web request in C#: (HttpWebRequest)WebRequest.Create(myUri). That is why I cann't use suggested Uri.OriginalString. I have to find the way to create request with escaped string. – user2018364 Jan 30 '13 at 12:57
  • There is an overload, `WebRequest.Create(string url)`, which takes a URL string. It might just create a `Uri` and then call `WebRequest.Create(Uri)`, but it's worth a shot. – Jim Mischel Jan 30 '13 at 14:11
  • Hi Jim, yes the WebRequest would create also URI from string. And it has the same effect: string will be unescaped. The problem is in unescaping. And it is done in URI and not in WebRequest, that is why I've asked about URI. – user2018364 Jan 30 '13 at 14:49
  • Another example of this same problem space: The Bing map control only takes Uris (not strings) for tile overlay images. When using something like Azure blob storage with Shared Access Signatures (which often contain '+' and '=' characters as part of the querystring of the image reference), there seems to be no way to create an escaped URI. Since the code is running in Silverlight, the other options involving medium trust and web.config don't seem to work either. – Jay Borseth Mar 25 '13 at 17:04
  • 1
    Have you tried Uri uri = new Uri(Uri.EscapeUriString(url))? – Sadhana Jul 22 '13 at 13:09

1 Answers1

1

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.

Arpit Jain
  • 455
  • 3
  • 8