Cleggy
Cleggy

Reputation: 725

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

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"?

Upvotes: 9

Views: 8403

Answers (1)

PinnyM
PinnyM

Reputation: 35533

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

Upvotes: 4

Related Questions