How to Validate URL in C#

I searched google for how to valicate URL in C# and most results say using a regular expression. Eventually I found Uri.TryCreate which is a built in method in C# to check if a string is a valid URL

Uri uri = null;

if (!Uri.TryCreate(url, UriKind.Absolute, out uri) || null == uri)

    //Invalid URL

    return false;

}

 

Reference:

http://msdn.microsoft.com/en-us/library/ms131572(v=VS.90).aspx

2 thoughts on “How to Validate URL in C#

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>