对于任何字符串值,Uri.TryCreate都返回true?

Sco*_*den 16 .net c# validation url uri

我正在尝试使用Uri.TryCreate方法验证Uri,当我使用无效字符串调用它时,该方法返回true.有什么想法吗?我的代码如下:

    private void button1_Click(object sender, EventArgs e)
    {
        Uri tempValue;
        if (Uri.TryCreate("NotAURL", UriKind.RelativeOrAbsolute, out tempValue))
        {
            MessageBox.Show("?");
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mat*_*hen 23

这是一个有效的相对URL.无效URI的示例是:

"http://example.com<>"
Run Code Online (Sandbox Code Playgroud)

如果您只想允许绝对URI,请使用UriKind.Absolute.

然后该示例将无法解析.

  • 它可能是网站上的相对URI.例如,如果您使用的是`http:// example.com /`,则相对URL指的是`http:// example.com/NotAURL`. (3认同)