Scott Scowden
Scott Scowden

Reputation: 1195

Uri.TryCreate returns true for any string value?

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("?");
        }
    }

Upvotes: 24

Views: 24995

Answers (1)

Matthew Flaschen
Matthew Flaschen

Reputation: 284927

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.

Upvotes: 28

Related Questions