Uri.TryCreate returns true for any string value?

↘锁芯ラ 提交于 2019-12-23 07:26:29

问题


I'm trying to validate a Uri using the Uri.TryCreate method and when I called it with an invalid string, the method returned true. Any ideas why? My code is below:

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

回答1:


That's a valid relative URL. An example of a invalid URI is:

"http://example.com<>"

If you want to allow only absolute URIs, use UriKind.Absolute.

The example will then fail to parse.



来源:https://stackoverflow.com/questions/4161247/uri-trycreate-returns-true-for-any-string-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!