Is there a method to validate URLs in .Net, ASP.Net, or ASP.Net MVC?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With