Using the URI Class

Shows you how to parse a URI using the URI class and how to form an absolute URI from a base URI and a relative URI.

Parsing a URI

Create a new Windows Application project. Add two textBoxes and a button. The first textBox, name it txtURI. The second, name it txtResult and set the multiline property to True. Name the button btnParse. Do the rest of adjustments so that the form looks like the one below.

First let’s not forget to add a reference to the System.Net namespace.

using System.Net;

Double click btnParse to get to the btnParse_Click() event. Here we shall use the following code:

txtResult.Clear();

Uri uri = new Uri(txtURI.Text);

txtResult.AppendText(“Absolute URI: ” + uri.AbsoluteUri + “\r\n”);

txtResult.AppendText(“Absolute Path: ” + uri.AbsolutePath + “\r\n”);

txtResult.AppendText(“Local path: ” + uri.LocalPath + “\r\n”);

txtResult.AppendText(“Scheme: ” + uri.Scheme + “\r\n”);

txtResult.AppendText(“Authority: ” + uri.Authority + “\r\n”);

txtResult.AppendText(“Host: ” + uri.Host + “\r\n”);

txtResult.AppendText(“Port: ” + uri.Port + “\r\n”);

txtResult.AppendText(“Fragment: ” + uri.Fragment + “\r\n”);

txtResult.AppendText(“Query: ” + uri.Query + “\r\n”);

First we clear txtResult from any prior parsing results.

In the second line we create a new Uri using the URL in the textBox. Next comes a sequence of uri properties:

AbsoluteUri property returns the complete URI. If you want you can call this the complete URI.

AbsolutePath returns the absolute path used by the server.

Local Path returns the local path of the file.

Scheme gets the scheme of the current URI (ex.: filehttpftp…).

Authority returns a string composed from the host name / IP address and port number of the server.

Host property returns a string with the host name / IP address of the server.

Port gets the port of the server.

Fragment returns the text followed by a marker (#) in an URI.

Query gives you the query information from the URI (ex.: ?id=5).

Here is an example result:

There’s another useful thing you can do using the Uri class:


Forming an absolute URI from a base URI and a relative URI

You can continue this on the same form. Add three textBoxes and a button. Name the first one txtBase, the second txtRelative and the third txtAbsolute. The name of the button will be btnForm. Now double click it. In the btnForm_Click() event use the following code:

Uri baseUri = new Uri(txtBase.Text);

Uri absoluteUri = new Uri(baseUri, txtRelative.Text);

txtAbsolute.Text = absoluteUri.ToString();

We create a new Uri named baseUri and then another named absoluteUri that is composed from the baseUri and the relative path contained in txtRelative.

Here’s an example result:

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top