Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a URL validator on .Net?

Is there a method to validate URLs in .Net, ASP.Net, or ASP.Net MVC?

like image 753
pupeno Avatar asked May 29 '09 19:05

pupeno


People also ask

How do you validate a URL?

You can use the URLConstructor to check if a string is a valid URL. URLConstructor ( new URL(url) ) returns a newly created URL object defined by the URL parameters. A JavaScript TypeError exception is thrown if the given URL is not valid.

What is a URL validator?

Link validation pings the destination of a URL and tests for errors. This helps avoid broken and invalid links in your published document, and is especially useful for bloggers.

How do you check a URL is valid or not in Java?

Using java.net.urlurl class to validate a URL. The idea is to create a URL object from the specified String representation. If we do not get exception while creating the object, we return true. Else we return false.


1 Answers

You can use the Uri.TryCreate to validate an URL:

public bool IsValidUri(string uri) {     Uri validatedUri;     return Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out validatedUri); } 

The comments suggest that TryCreate just moves the exception handling one level down. However, I checked the source code and found that this is not the case. There is no try/catch inside TryCreate, it uses a custom parser which should not throw.

like image 86
Dirk Vollmar Avatar answered Oct 05 '22 00:10

Dirk Vollmar