问题
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?
回答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.
来源:https://stackoverflow.com/questions/39835099/is-there-a-way-to-keep-dot-segments-in-url-using-uri-class