0

In ASP.net what's the best way I can parse a string to determine if it's a valid URL?

2 Answers 2

4

Call Uri.TryCreate.

For example:

Uri parsedUri;
if (!Uri.TryCreate(input, UriKind.Absolute, out parsedUri)) {
    //Bad!
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use a regular expression

3 Comments

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)
So why not sit down with the spec and write one that does?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.