Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Mar 22

Written by: Steve Gray
3/22/2011 5:55 PM  RssIcon

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
  1. Module Module1
  2.  
  3.     Sub Main()
  4.         TryCreate1()
  5.         TryCreate2()
  6.         TryCreate3()
  7.     End Sub
  8.  
  9.     Sub TryCreate1()
  10.         'demonstrates Uri.TriCreate(Uri,String,Uri)
  11.         Dim UriIn As Uri = New Uri("http://devshed.us/")
  12.         Dim strPage As String = "/links.aspx"
  13.         Dim UriOut As Uri = Nothing
  14.  
  15.         If Uri.TryCreate(UriIn, strPage, UriOut) Then
  16.             Debug.Print(UriOut.ToString)
  17.         Else
  18.             Throw New Exception("Invalid URI")
  19.         End If
  20.     End Sub
  21.     Sub TryCreate2()
  22.         'demonstrates Uri.TriCreate(String,Urikind, Uri)
  23.         'this form doesn't create anything. It just tells us if the URI that we're passing
  24.         'in the first parameter is Relative or Absolute. Or valid at in any form
  25.  
  26.         'test to see if this string is a valid URI
  27.         Dim strUrl As String = "http://devshed.us/"
  28.  
  29.         'placeholder for our output
  30.         Dim UriOut As Uri = Nothing
  31.  
  32.         'see if our input qualifies as an Absolute URI
  33.         If Uri.TryCreate(strUrl, UriKind.Absolute, UriOut) Then
  34.             'yes
  35.             Debug.Print(UriOut.ToString & " is absolute")
  36.         Else
  37.             'no
  38.             Debug.Print(strUrl & " is not absolute")
  39.         End If
  40.  
  41.         'see if our input qualifies as a Relative URI
  42.         If Uri.TryCreate(strUrl, UriKind.Relative, UriOut) Then
  43.             'yes
  44.             Debug.Print(UriOut.ToString & " is Relative")
  45.         Else
  46.             'no
  47.             Debug.Print(strUrl & " is not Relative")
  48.         End If
  49.  
  50.         'same as above, but this time with a string that is Relative
  51.         strUrl = "/index.aspx"
  52.         If Uri.TryCreate(strUrl, UriKind.Absolute, UriOut) Then
  53.             Debug.Print(UriOut.ToString & " is absolute")
  54.         Else
  55.             Debug.Print(strUrl & " is not absolute")
  56.         End If
  57.  
  58.         If Uri.TryCreate(strUrl, UriKind.Relative, UriOut) Then
  59.             Debug.Print(UriOut.ToString & " is Relative")
  60.         Else
  61.             Debug.Print(strUrl & " is not Relative")
  62.         End If
  63.     End Sub
  64.     Sub TryCreate3()
  65.         'demonstrates Uri.TriCreate(Uri,Uri,Uri)
  66.         'concatonates the first two URIs into the third
  67.  
  68.         'get a base uri
  69.         Dim BaseUri As Uri = New Uri("http://devshed.us/")
  70.  
  71.         'note that we have to specify the urikind
  72.         Dim RelativeUri As New Uri("/links.aspx", UriKind.Relative)
  73.  
  74.         'placeholder for the return val
  75.         Dim UriOut As Uri = Nothing
  76.  
  77.         'add the two together
  78.         If Uri.TryCreate(BaseUri, RelativeUri, UriOut) Then
  79.             Debug.Print(UriOut.ToString)
  80.         Else
  81.             Throw New Exception("Invalid URI")
  82.         End If
  83.  
  84.     End Sub
  85.  
  86. End Module

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us