.NET Uri.ToString()破坏了URL?

时间:2016-11-23 16:17:55

标签: c# .net url uri

我注意到,如果您在后面的URI是带有百分比编码空格(%20)的网址时调用Uri.ToString(),则结果会出现格式错误的网址:

// result: "http://example.com/test segment"
new Uri("http://example.com/test%20segment").ToString();

由于URL is URI(相反的陈述不正确)和带有非编码空格的网址is not valid,使用Uri作为URL的容器似乎不安全。

这是正确的行为吗?

1 个答案:

答案 0 :(得分:3)

根据MSDN

// result: "http://example.com/test segment"
new Uri("http://example.com/test%20segment").ToString();

// result: "http://example.com/test%20segment"
new Uri("http://example.com/test%20segment").OriginalString;