Uri Class, UriBuilder
This page was last reviewed on Jun 21, 2024.
Dot Net Perls
Uri. In VB.NET programs, we often need to create and store URIs, or URLs, to remote resources like web servers. This can be done with the Uri and UriBuilder Classes.
To test if a URL is valid, we can use the Uri.TryCreate method. This tester-doer method returns True if the string can be parsed as a URL.
Example. One benefit to storing URLs in Uri objects is that we can access properties on the Uri. These properties can return important parts like the Host or the Scheme (like http).
Tip With a Uri object instance, we do not need to parse parts of the Uri ourselves. We can use helper functions to access the URI parts.
Module Module1 Sub Main() Dim uri As Uri = new Uri("https://www.dotnetperls.com/") ' Print properties of Uri instance. Console.WriteLine("AbsolutePath = {0}", uri.AbsolutePath) Console.WriteLine("AbsoluteUri = {0}", uri.AbsoluteUri) Console.WriteLine("Authority = {0}", uri.Authority) Console.WriteLine("DnsSafeHost = {0}", uri.DnsSafeHost) Console.WriteLine("Fragment = {0}", uri.Fragment) Console.WriteLine("Host = {0}", uri.Host) Console.WriteLine("HostNameType = {0}", uri.HostNameType) Console.WriteLine("IsAbsoluteUri = {0}", uri.IsAbsoluteUri) Console.WriteLine("IsDefaultPort = {0}", uri.IsDefaultPort) Console.WriteLine("IsFile = {0}", uri.IsFile) Console.WriteLine("IsLoopback = {0}", uri.IsLoopback) Console.WriteLine("IsUnc = {0}", uri.IsUnc) Console.WriteLine("LocalPath = {0}", uri.LocalPath) Console.WriteLine("OriginalString = {0}", uri.OriginalString) Console.WriteLine("PathAndQuery = {0}", uri.PathAndQuery) Console.WriteLine("Port = {0}", uri.Port) Console.WriteLine("Query = {0}", uri.Query) Console.WriteLine("Scheme = {0}", uri.Scheme) Console.WriteLine("Segments = {0}", String.Join(",", uri.Segments)) Console.WriteLine("UserEscaped = {0}", uri.UserEscaped) Console.WriteLine("UserInfo = {0}", uri.UserInfo) End Sub End Module
AbsolutePath = / AbsoluteUri = https://www.dotnetperls.com/ Authority = www.dotnetperls.com DnsSafeHost = www.dotnetperls.com Fragment = Host = www.dotnetperls.com HostNameType = Dns IsAbsoluteUri = True IsDefaultPort = True IsFile = False IsLoopback = False IsUnc = False LocalPath = / OriginalString = https://www.dotnetperls.com/ PathAndQuery = / Port = 443 Query = Scheme = https Segments = / UserEscaped = False UserInfo =
TryCreate. Is a String a valid URL? We can determine this with Uri.TryCreate, and as a bonus we can get the constructed Uri object as a return parameter.
Module Module1 Sub Main() ' Part 1: use Uri.TryCreate on valid url. Dim uri1 As Uri = Nothing If Uri.TryCreate("http://www.dotnetperls.com/", UriKind.Absolute, uri1) Console.WriteLine("1 = {0}", uri1) End If ' Part 2: handle an invalid Url with Uri.TryCreate. Dim uri2 As Uri = Nothing If Uri.TryCreate("http:dotnetperls-com", UriKind.Absolute, uri2) Console.WriteLine("2 = {0}", uri2) ' Not reached. End If End Sub End Module
1 = http://www.dotnetperls.com/
UriBuilder. It is sometimes better to create a Uri object by using the UriBuilder Class. We can set parts like the Host, Path, and Scheme to modify the returned URL.
Tip The UriBuilder handles the syntax of the Uri scheme, such as the path separator and the scheme delimiter.
Module Module1 Sub Main() ' Use UriBuilder constructor. Dim u1 As UriBuilder = New UriBuilder("http", "www.dotnetperls.com") Console.WriteLine($"UriBuilder: {u1}") ' Use UriBuilder properties. Dim u2 As UriBuilder = New UriBuilder() u2.Host = "www.dotnetperls.com" u2.Path = "uribuilder" u2.Scheme = "http" Console.WriteLine($"UriBuilder: {u2}") ' Convert to Uri. Dim uri As Uri = u2.Uri Console.WriteLine($" Uri: {uri}") End Sub End Module
UriBuilder: http://www.dotnetperls.com/ UriBuilder: http://www.dotnetperls.com/uribuilder Uri: http://www.dotnetperls.com/uribuilder
Summary. For complex VB.NET programs, using Uri and UriBuilder to manage URLs is a good idea as it can reduce possible syntax bugs in the resulting URLs. It can lead to more robust programs.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 21, 2024 (new).
© 2007-2024 Sam Allen.