new Uri decoding relative path

The following code...

string date = DateTime.UtcNow.ToString("o");
Console.WriteLine(date);
string encodedDate = WebUtility.UrlEncode(date);
Console.WriteLine(encodedDate);
Uri uri = new Uri("https://mywebsite/");
Console.WriteLine(uri.ToString());
string relativePath = $"/mM0jpk613J5lzY00c3EaxQ%3D%3D/{encodedDate}";
Console.WriteLine(relativePath);
uri = new Uri(uri, relativePath);
Console.WriteLine(uri.ToString());

Produces the following output is:-

2016-03-07T08:17:38.5247330Z
2016-03-07T08%3A17%3A38.5247330Z
https://mywebsite/
/mM0jpk613J5lzY00c3EaxQ%3D%3D/2016-03-07T08%3A17%3A38.5247330Z
https://mywebsite/mM0jpk613J5lzY00c3EaxQ%3D%3D/2016-03-07T08:17:38.5247330Z

I was expecting the last line to be

https://mywebsite/mM0jpk613J5lzY00c3EaxQ%3D%3D/2016-03-07T08%3A17%3A38.5247330Z

Why does Uri unescape the relative path? A work around seems to be to encode the relativepath twice, but why?

Is there an alternative method I should be using to create this url?

EDIT: I updated the code to display even more odd behavior, the first part of the relative uri remains encoded, but the second does not!


URI is an object and will represent the link in its clean/unescaped form. If you are looking for retrieving it the way it was added, use the below property.

uri.OriginalString
链接地址: http://www.djcxy.com/p/64662.html

上一篇: 如何通过共享主机上的cPanel部署Ruby on Rails应用程序?

下一篇: 新的Uri解码相对路径