3

When I create a uri with dot segments:

var uri = new Uri("http://localhost/../../g");

The uri class removes the ../ segments and the result uri becomes:

http://localhost/g

When there is a path before the dots:

var uri = new Uri("http://localhost/a/b/c/./../../g");
// Result: http://localhost/a/g

Looks like the Uri class is following the standart (Page 33 - remove_dot_segments), but is there any way to keep dot segments instead of automatically resolving the target uri, using Uri class? Or do I need a custom implementation?

Selman Genç
  • 100,147
  • 13
  • 119
  • 184

1 Answers1

1

If you are using HTTP then no, it will always escape them if you use the Uri class. It escapes for all of the following: file, http, https, net.pipe, and net.tcp

If you are using something like ftp then it won't escape, but it sounds like that isn't an option for you.

From MSDN's documentation:

As part of canonicalization in the constructor for some schemes, escaped representations are compacted. The schemes for which URI will compact escaped sequences include the following: file, http, https, net.pipe, and net.tcp. For all other schemes, escaped sequences are not compacted. For example: if you percent encode the two dots ".." as "%2E%2E" then the URI constructor will compact this sequence for some schemes. For example, the following code sample shows a URI constructor for the http scheme.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • that's unfortunate. do you know of any 3rd party library that does what I want? – Selman Genç Oct 03 '16 at 15:46
  • Hrrm possibly, what exactly is it that you want to do? If you want to return a `Uri` object for a `http://...` Uri that has dots in it I think you are out of luck. If you just want a string representation I'm sure you could work something out. – Abe Miessler Oct 03 '16 at 15:52
  • I would like to keep dots, and then create a request to that exact url, so I'm guessing I won't just need a custom uri implementation cause WebRequest class is following the same standarts. – Selman Genç Oct 03 '16 at 16:01
  • It looks like the `WebRequest` constructor takes a string for the URL to hit. Seems like you could just skip using the `Uri` class and pass in the string you want to target, correct? – Abe Miessler Oct 03 '16 at 16:31
  • Ahh, I was wondering if it uses the `Uri` class behind the scenes. – Abe Miessler Oct 03 '16 at 16:32