【问题标题】:Bug? System.UriFormatException: Invalid URI: The URI scheme is not valid漏洞? System.UriFormatException:无效的 URI:URI 方案无效
【发布时间】:2020-08-28 09:26:05
【问题描述】:

字符串相同,但作为变量传递时无效?

这到底是怎么回事?是语言错误吗?我在 C# .Net Core 中运行它

var postUrl = "​http://www.contoso.com";
var postUri = new Uri("http://www.contoso.com"); // works
var uri = new Uri(postUrl); // does not work

【问题讨论】:

    标签: c# .net-core uri


    【解决方案1】:

    如果你拉你的头发,那是因为在 postUrl 中第一次打开引号后有空格。请删除该空间,您的错误将消失。

    【讨论】:

    • 眼光敏锐!事实上,这是一个 Unicode 字符 'ZERO WIDTH SPACE' (U+200B)
    【解决方案2】:

    通过使用解决了这个问题。

    var postUrl = "​http://www.contoso.com";
    var uriBuilder = new UriBuilder(postUrl);
    var uri = uriBuilder.Uri
    

    还在想不通吗?

    只是一个程序员做他的工作的日常。

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 2021-11-28
      • 2020-06-27
      • 1970-01-01
      • 2016-01-05
      相关资源
      最近更新 更多