Mar
22
Written by:
Steve Gray
3/22/2011 5:55 PM
Recently I needed to concatenate two strings and form a URI. I had the base stored in the site config; then we needed to add the page onto that. I wanted to be sure that I didn’t have errors if the user added or didn’t add ‘/’ to the URI in the config so I started with Path.combine. <smiles> That worked (sort of) but it used the wrong ‘/’ and it generated an error.
Enter URI.TriCreate. According to the documentation this would do the trick. But the documentation didn’t have any examples; and I couldn’t figure out the syntax. How bad is the documentation if you read all the way through it and still don’t have a clue?
At any rate, I searched on line for a while and put it all together. Here are code sample for the three overloads.
A few definitions so that you don’t have the same issues that I did:
- URI: Uniform Resource Identifier. A string of characters used to identify a name or a resource on the Internet.
- UriKind: URIs can be Absolute or Relative. “www.devshed.us” is Absolute. “/links.aspx” is relative.
Code Snippet
- Module Module1
-
- Sub Main()
- TryCreate1()
- TryCreate2()
- TryCreate3()
- End Sub
-
- Sub TryCreate1()
- 'demonstrates Uri.TriCreate(Uri,String,Uri)
- Dim UriIn As Uri = New Uri("http://devshed.us/")
- Dim strPage As String = "/links.aspx"
- Dim UriOut As Uri = Nothing
-
- If Uri.TryCreate(UriIn, strPage, UriOut) Then
- Debug.Print(UriOut.ToString)
- Else
- Throw New Exception("Invalid URI")
- End If
- End Sub
- Sub TryCreate2()
- 'demonstrates Uri.TriCreate(String,Urikind, Uri)
- 'this form doesn't create anything. It just tells us if the URI that we're passing
- 'in the first parameter is Relative or Absolute. Or valid at in any form
-
- 'test to see if this string is a valid URI
- Dim strUrl As String = "http://devshed.us/"
-
- 'placeholder for our output
- Dim UriOut As Uri = Nothing
-
- 'see if our input qualifies as an Absolute URI
- If Uri.TryCreate(strUrl, UriKind.Absolute, UriOut) Then
- 'yes
- Debug.Print(UriOut.ToString & " is absolute")
- Else
- 'no
- Debug.Print(strUrl & " is not absolute")
- End If
-
- 'see if our input qualifies as a Relative URI
- If Uri.TryCreate(strUrl, UriKind.Relative, UriOut) Then
- 'yes
- Debug.Print(UriOut.ToString & " is Relative")
- Else
- 'no
- Debug.Print(strUrl & " is not Relative")
- End If
-
- 'same as above, but this time with a string that is Relative
- strUrl = "/index.aspx"
- If Uri.TryCreate(strUrl, UriKind.Absolute, UriOut) Then
- Debug.Print(UriOut.ToString & " is absolute")
- Else
- Debug.Print(strUrl & " is not absolute")
- End If
-
- If Uri.TryCreate(strUrl, UriKind.Relative, UriOut) Then
- Debug.Print(UriOut.ToString & " is Relative")
- Else
- Debug.Print(strUrl & " is not Relative")
- End If
- End Sub
- Sub TryCreate3()
- 'demonstrates Uri.TriCreate(Uri,Uri,Uri)
- 'concatonates the first two URIs into the third
-
- 'get a base uri
- Dim BaseUri As Uri = New Uri("http://devshed.us/")
-
- 'note that we have to specify the urikind
- Dim RelativeUri As New Uri("/links.aspx", UriKind.Relative)
-
- 'placeholder for the return val
- Dim UriOut As Uri = Nothing
-
- 'add the two together
- If Uri.TryCreate(BaseUri, RelativeUri, UriOut) Then
- Debug.Print(UriOut.ToString)
- Else
- Throw New Exception("Invalid URI")
- End If
-
- End Sub
-
- End Module
As always, I welcome your comments!