Check Url if valid in C#
To check if a Url is valid in C# you can useUri.TryCreate()
method which creates a new Uri and it does not throw an exception if the Uri cannot be created, it will return a bool
if it was created successfully. Im this page you will learn how to validate for HTTP
& HTTPS
.Example
using System;
public class Program
{
public static void Main()
{
string uriName = @"https:\\www.google.com";
Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
Console.WriteLine("This Url is valid {0}", result);
}
}
Output
This Url is valid True