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("?");
}
}
TryCreate(Uri, String, Uri)Creates a new Uri using the specified base and relative String instances.
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.
UriKind.Absolute: Absolute URIs are Complete url, which start with protocol name Example: new Uri("http://www.testdomain.com/index.html ", UriKind.Absolute)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With