gpt4 book ai didi

c# - URL 验证 IsWellFormedUriString 始终返回 true

转载 作者:行者123 更新时间:2023-11-30 19:05:30 26 4
gpt4 key购买 nike

我使用下面的代码来验证一个 url,但是在这两种情况下的问题检查返回 true。我应该如何在第二个选项中得到 false?

这里 bool 为真

 bool isWellFormedUriString = Uri.IsWellFormedUriString("http://stackoverflow.com/", UriKind.RelativeOrAbsolute);

这里也是如此

 bool isWellFormedUriString = Uri.IsWellFormedUriString("ddddd", UriKind.RelativeOrAbsolute);

现在当我这样做的时候_uri = new Uri(_url); 我遇到异常....

最佳答案

字符串 ddddd 可能是完整 URI 的有效“部分”,因此使用 RelativeRelativeOrAbsolute 返回 true。

尝试将第二个参数更改为 UriKind.Absolute:

bool isWellFormedUriString  // returns true
= Uri.IsWellFormedUriString("http://stackoverflow.com/", UriKind.Absolute);

bool isWellFormedUriString // returns false
= Uri.IsWellFormedUriString("ddddd", UriKind.Absolute);

来自docs :

Absolute URIs are characterized by a complete reference to the resource (example: http://www.contoso.com/index.html), while a relative Uri depends on a previously defined base URI (example: /index.html).

关于c# - URL 验证 IsWellFormedUriString 始终返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com