Most of today’s applications connect in one form or another to resources over a network. To do this we usually use URIs (Uniform Resource Identifiers). Therefore, as a C# developer you’ll often be in a position to use URIs in your applications. To do this we can theoretically represent or manage URI data with strings. So if I want to use “http://danpatrascu.com” in my program I can simply use it as a string like I basically did right now. However, most of the time your applications will need to implement certain logic based certain parts of the URI or manipulate/change parts of the URI. Since you mostly won’t know the exact URI when you write your code using string methods will become very painful. Thankfully, we have better ways to work with URIs in C#.
Meet the Uri class
Creating URIs in C#
URIs in C# can be represented through the Uri class. To successfully create a new Uri instance you will need to provide a string in URI format. If the format is not correct you’ll get an UriFormatException.
Uri uri = new Uri(“http://danpatrascu.com/using-ocelot-in-a-dockerized-application/”);
Uri uri2 = new Uri(“someString”); //throws UriFormatException
If you’re not sure beforehand whether the string you want to use for URI creation is in correct format or not, you can use the static TryCreate() method on the URI class. This will check if the URI is correct and if it is, the method will return true and will also create a new instance of URI based on the provided string. This would look similar to this piece of code:
1 2 3 4 5 6 | Uri uri2; var createUri = Uri.TryCreate("dan", UriKind.Absolute, out uri2); if (createUri) { Console.WriteLine(uri2); } |
Ok, now what?
Well, you have a URI. Jokes aside the URI class is especially useful when you need to take decisions based on URi parts or simply change them. The newly created Uri object will expose a lot of properties that represent the different parts of the resource identifier. You can for instance access the host, the path and query, the different segments, the scheme and so on. And all this without the needs to think about forward slashes or other URI specific characters that might be present in the identifier you’re working with.
1 2 3 4 5 6 | Uri uri = new Uri("http://danpatrascu.com/using-ocelot-in-a-dockerized-application/"); Console.WriteLine(uri.Authority); Console.WriteLine(uri.Host); Console.WriteLine(uri.Port); Console.WriteLine(uri.PathAndQuery); Console.WriteLine(uri.Scheme); |
Having access to this type of information may prove very useful to you as a programmer since there are a lot of decisions that you can take based on all these pieces of information. So deconstructing a URI into its different parts is easy. but what about the revers process?
Also meet the UriBuilder class!
When working with URIs in C# you will often encounter scenarios in which you’ll need to construct a Uri object based on pieces of information that you might receive from different methods. The UriBuilder class is very useful in this regard since its responsibility is exactly this: construct a URI based on its different parts. And it’s very easy to use!
1 2 3 4 5 | UriBuilder builder = new UriBuilder(); builder.Scheme = "http"; builder.Host = "danpatrascu.com"; builder.Path = "using-ocelot-in-a-dockerized-application/"; Console.WriteLine(builder.Uri); |
Now you can do whatever you want with your new URI!
Use cases
When I started to learn programming one of the challenges I had when going through articles like this is that I would usually not find any information about use cases. So when will I most probably need to use this classes? I don’t want to make the same mistake in my articles so here are a few use cases for the Uri class.
A first scenario where using the Uri and UriBuiler classes is when you know a filename, the host on a network where that filename resides and need to access that file. In this case you would need to convert something like “C:\someFile.txt” into something like “file:///hostname/someFile.txt”. Depending on what exactly you need to to working with Uri and UriBuilder will come in handy in this scenario.
Another use case would be to modify the host name (or any other URI) in a location header which is part of an HTTP response.
Conversions from relative paths to absolute paths can also be achieved easily using these classes.
If you have anything to say, don’t be shy and drop a comment. Also, if you find this article useful feel free to share it within your network. There might be other peers of you that might find it useful as well. Cheers!
How useful was this post?
Click on a star to rate it!
Average rating / 5. Vote count:
Dan Patrascu-Baba
Latest posts by Dan Patrascu-Baba (see all)
- Configuration and environments in ASP.NET Core - 25/11/2019
- GraphQL in the .NET ecosystem - 19/11/2019
- A common use case of delegating handlers in ASP.NET API - 12/11/2019