How to stop System.Uri from unencoding URL?

How to stop the System.Uri class from unencoding the encoded URL passed to 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 guarantee that after creating the Uri instance, Uri.AbsoluteUri will return "http://foo.bar/foo%2FBar"?

+6
source share
1 answer

The Uri class is not used to create an escaped URI, although you can use uri.OriginalString to retrieve the string used for initialization. You should probably use UrlEncode if you need to safely transcode the URI.

In addition, according to http://msdn.microsoft.com/en-us/library/9zh9wcb3.aspx, the dontEscape parameter in the Uri initializer is deprecated and will always be false.

UPDATE:

Someone seems to have found a way (hack) to do this - Getting a slash url with a url

+4
source

Source: https://habr.com/ru/post/912287/


All Articles