Is there a way to keep dot segments in url using Uri class?

假装没事ソ 提交于 2021-01-27 13:21:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!