Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop System.Uri from unencoding a URL?

Tags:

c#

.net

How do I stop the System.Uri class from unencoding an encoded URL passed into its constructor? Consider the following code:-

Uri uri = new Uri("http://foo.bar/foo%2FBar");
Console.WriteLine(uri.AbsoluteUri);
uri = new Uri("http://foo.bar/foo%2FBar", false);
Console.WriteLine(uri.AbsoluteUri);
uri = new Uri("http://foo.bar/foo%2FBar", true);
Console.WriteLine(uri.AbsoluteUri);

In each case the output is "http://foo.bar/foo/bar". How can I ensure that after instantiating a Uri instance, Uri.AbsoluteUri will return "http://foo.bar/foo%2FBar"?

like image 499
Cleggy Avatar asked Apr 03 '12 05:04

Cleggy


1 Answers

The Uri class isn't used to generate an escaped URI, although you can use uri.OriginalString to retrieve the string used for initialization. You should probably be using UrlEncode if you need to reencode a URI safely.

Also, as per http://msdn.microsoft.com/en-us/library/9zh9wcb3.aspx the dontEscape parameter in the Uri initializer has been deprecated and will always be false.

UPDATE:

Seems someone found a way (hack) to do this - GETting a URL with an url-encoded slash

like image 185
PinnyM Avatar answered Sep 24 '22 17:09

PinnyM