Pronunciation / adjective
Bringing something good and unforseen.
    Skip Navigation Links > Code Repository
   

Resolving URI Paths - How to Build a URL



 
Return

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:

(This code is within a datareader's While Read statement)
  1. EntryId = DataReader.Item("pk_BlogEntryId").ToString()
  2. EntryUri = Request.Url.GetLeftPart(UriPartial.Authority) & Page.ResolveUrl("Technology-Blog.aspx") & "?entid=" & EntryId


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.
  1. ResolvedUrl = System.Web.VirtualPathUtility.ToAbsolute("~/MyFolder/MyPage.aspx")


Return
Microsoft Certified Professional   © 2017 Fortunate.  All rights reserved.
contact: justin@aboutfortunate.com