SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers

System.Uri Class properties and parse URL names

System.Uri class provides an object representation of a uniform resource identifier (URI) and provides easy access to the parts of the URI. A URI is actually represents any resource available in an intranet or internet environment. URI points to the location of the resource.

By using the properties of the URI class, it is very easy to parse the URI into its sub-components.

You can easily create an instance of the System.Uri by the following line of code

Dim uri As New System.Uri("/articles/article.aspx?articleid=27")

One way of creating a URI object is using the Request object of the System.Web.HttpRequest namepace using the below code

Dim uri As System.Uri = Request.Url
Dim uri As System.Uri = Request.UrlReferrer

And now we can parse the URI into its components by using the class properties.

Property Value for sample
AbsolutePath /articles/article.aspx
AbsoluteUri /articles/article.aspx?articleid=27
Authority www.kodyaz.com
Host www.kodyaz.com
LocalPath /articles/article.aspx
OriginalString /articles/article.aspx?articleid=27
PathAndQuery /articles/article.aspx?articleid=27
Port 80
Query ?articleid=27
IsFile False
Scheme http
SchemeDelimiter ://

Let's make an other sample for  file shared over network.

Dim uri As New System.Uri("\\istw1947\share\cdonts.dll")

Property Value for sample
AbsolutePath /share/cdonts.dll
AbsoluteUri file://istw1947/share/cdonts.dll
Authority istw1947
Host istw1947
LocalPath \\istw1947\share\cdonts.dll
OriginalString \\istw1947\share\cdonts.dll
PathAndQuery /share/cdonts.dll
Port -1
IsFile True
Scheme file
SchemeDelimiter ://

I guess the above samples are at least a little bit self explaining.

OriginalString is the value passed to the constructor of the URI.

Authority is the Domain Name System (DNS) host name or the IP address of the URI resource which is the server name of the shared file or the domain name of the server hosting the resource.

You will notice that the IsFile boolean property as well as the Scheme differs between the two samples. For the web page the Scheme is http and the IsFile property is False. For the shared file the Scheme is file and the IsFile property is True.

Query property of the URI returns any query information included in the URI. For a web page as a URI, this points to the parameters passed to the page.

Port is the port number of this URI. For the web page sample the port property returns 80, but -1 for the shared folder sample.

There is many other properties of thte URI class that I have not listed here. One of them is IsLoopback property indicating whether the URI references the localhost.

Visual Studio

Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.