【问题标题】:.NET Uri.ToString() ruins the URL?.NET Uri.ToString() 破坏了 URL?
【发布时间】:2016-11-23 16:17:54
【问题描述】:

我注意到,如果您在 URI 后面是带有百分比编码空格 (%20) 的 URL 时调用 Uri.ToString(),那么您将得到格式错误的 URL:

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

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

这是正确的行为吗?

【问题讨论】:

  • 推测ToString() 正在按照父编码规范解析%20,并确定它应该是一个空格。
  • MSDN 表示它应该返回带有常规空格的字符串。如果您想要 %20 而不是空格,请使用 .OriginalString。
  • 你指出的文档说.ToString()返回空格,而.OriginalString返回%20

标签: c# .net url uri


【解决方案1】:

根据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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2013-01-18
    • 2017-02-24
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2022-10-21
    相关资源
    最近更新 更多