.NET Tips and Tricks

Blog archive

Manipulating URLs on the Server

There's nothing stopping you from working with URLs in code using standard string functions. If you want to create a URL, you should use the URL object. On the other hand, if you want to analyze a URL -- or, more properly, a URI -- you should use the Uri class.

To use the Uri class, you just instantiate it, passing a URI. Once you've done that, you can check whether the URI's scheme name (ftp, http and so on) is valid, that the host name is a valid DNS name, or even dismantle the URI to get its individual components (to get, for example, the port or host name).

You can even use the Uri object to compare two URLs to see if they are significantly different (the Uri object is smart enough to recognize that two URLs are identical if one URL omits a port number while the other one specifies the default).

Posted by Peter Vogel on 02/13/2018

comments powered by Disqus


Subscribe on YouTube