Search code examples
c#.netasp.netvb.neturl-validation

Validate an url with parameters


I’m trying to determine whether or not a given string is a valid url or not. And in my scenario, the url can have parameters:

  • www.example.com -> OK
  • example.test -> OK (although there’s no .test TLD)
  • example.com/page.htm?abc=123 -> OK
  • xxx/xxx.jpg -> Not OK
  • xxx -> Not OK

I’ve tried the Uri.TryCreate method, Uri.TryCreate(url, UriKind.Absolute, null);, but it accepts pretty much anything that has an http:// prefix, i.e. “http://xxx/” is OK.

I can’t use an HTTP request to check/ping the site for performance reasons.

Any suggestions?


Solution

  • It sounds like you want to call Uri.TryCreate(url, UriKind.Absolute, out result), then check that result.HostName contains a .