In ASP.net what's the best way I can parse a string to determine if it's a valid URL?
+4
A:
Call Uri.TryCreate
.
For example:
Uri parsedUri;
if (!Uri.TryCreate(input, UriKind.Absolute, out parsedUri)) {
//Bad!
}
SLaks
2010-10-26 12:54:06
_No_, **don't**.
SLaks
2010-10-26 13:04:33
Any reasons why?
m.edmondson
2010-10-26 13:07:29
Most regular expressions that you'll find will not fully conform to the URL spec. (eg, inline basic auth, `.museam`, full sets of domain name characters)
SLaks
2010-10-26 13:08:24
So why not sit down with the spec and write one that does?
m.edmondson
2010-10-26 13:08:55
Because you'll end up with an enormous and complicated regex (which you'll need to debug) that duplicates functionality already available in `Uri.TryCreate`.
SLaks
2010-10-26 13:09:58
Okay fair comment
m.edmondson
2010-10-26 13:11:18