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”?
The
Uriclass isn’t used to generate an escaped URI, although you can useuri.OriginalStringto 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
dontEscapeparameter 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