The answers provided thusfar do not check for a scheme, allowing all kinds of unwanted input, which could make you vulnerable for javascript injection (see the comment of TheCloudlessSky).
An URI is just a unique identification of a object. "C:\Test" is a valid URI.
In my project I used the following code:
/// <summary>
/// Validates a URL.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private bool ValidateUrl(string url)
{
Uri validatedUri;
if (Uri.TryCreate(url, UriKind.Absolute, out Uri validatedUri)) //.NET URI validation.
{
//If true: validatedUri contains a valid Uri. Check for the scheme in addition.
return (validatedUri.Scheme == Uri.UriSchemeHttp || validatedUri.Scheme == Uri.UriSchemeHttps);
}
return false;
}
Define which schemes you will allow and change the code accordingly.