Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uri.TryCreate returns true for any string value?

Tags:

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("?");
        }
    }
like image 650
Scott Scowden Avatar asked Nov 12 '10 02:11

Scott Scowden


People also ask

What does URI TryCreate do?

TryCreate(Uri, String, Uri)Creates a new Uri using the specified base and relative String instances.

What is URI Iswellformeduristring?

Indicates whether the string is well-formed by attempting to construct a URI with the string and ensures that the string does not require further escaping.

What is UriKind absolute?

UriKind.Absolute: Absolute URIs are Complete url, which start with protocol name Example: new Uri("http://www.testdomain.com/index.html ", UriKind.Absolute)


1 Answers

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.

like image 139
Matthew Flaschen Avatar answered Sep 23 '22 09:09

Matthew Flaschen