Resolving URI Paths - How to Build a URL

Resolving URI Paths - How to Build a URL

Sometimes using the tilde "~" character in .net for building a URI just won't work for you. One example would be when you want to create a list of links to various pages and you want those links to work from any server. Maybe you're developing on http://localhost/myapp, testing on (a staging server with a specified port#) http://10.1.1.2:5543, and deploying to http://www.mysite.com. There are two different ways built into .NET to help with this. The first requires two methods that may be used to build a base URI that will create proper links under any of these circumstances:

For example, here is how I build the links for the RSS feed on this site:

If you don't have access to the Request and/or Page objects then System.Web contains a "VirtualPathUtility" method. Better yet this method is static (shared in VB) so you don't have to declare the object to use it.