C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: The Uri class provides a much easier way to access parts of the URI and also manipulate and combine URIs.
Then: The Print method accesses the public properties on the Uri instance and prints them to the screen.
And: In the output, you can see the different parts of the 3 constructed Uri objects.
C# program that demonstrates Uri class
using System;
class Program
{
static void Main()
{
// Construct three Uri objects.
Uri uri1 = new Uri("http://dotnetCodex.com/");
Uri uri2 = new Uri(uri1, "/datagridview-tips");
Uri uri3 = new Uri("http://dotnetCodex.com/Test/Exists.html?good=true");
// Write properties.
Print(uri1);
Print(uri2);
Print(uri3);
}
static void Print(Uri uri)
{
// 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);
Console.WriteLine(new string('-', 40));
}
}
Output
AbsolutePath = /
AbsoluteUri = http://dotnetCodex.com/
Authority = dotnetCodex.com
DnsSafeHost = dotnetCodex.com
Fragment =
Host = dotnetCodex.com
HostNameType = Dns
IsAbsoluteUri = True
IsDefaultPort = True
IsFile = False
IsLoopback = False
IsUnc = False
LocalPath = /
OriginalString = http://dotnetCodex.com/
PathAndQuery = /
Port = 80
Query =
Scheme = http
Segments = /
UserEscaped = False
UserInfo =
----------------------------------------
AbsolutePath = /datagridview-tips
AbsoluteUri = http://dotnetCodex.com/datagridview-tips
Authority = dotnetCodex.com
DnsSafeHost = dotnetCodex.com
Fragment =
Host = dotnetCodex.com
HostNameType = Dns
IsAbsoluteUri = True
IsDefaultPort = True
IsFile = False
IsLoopback = False
IsUnc = False
LocalPath = /datagridview-tips
OriginalString = http://dotnetCodex.com/datagridview-tips
PathAndQuery = /datagridview-tips
Port = 80
Query =
Scheme = http
Segments = /,datagridview-tips
UserEscaped = False
UserInfo =
----------------------------------------
AbsolutePath = /Test/Exists.html
AbsoluteUri = http://dotnetCodex.com/Test/Exists.html?good=true
Authority = dotnetCodex.com
DnsSafeHost = dotnetCodex.com
Fragment =
Host = dotnetCodex.com
HostNameType = Dns
IsAbsoluteUri = True
IsDefaultPort = True
IsFile = False
IsLoopback = False
IsUnc = False
LocalPath = /Test/Exists.html
OriginalString = http://dotnetCodex.com/Test/Exists.html?good=true
PathAndQuery = /Test/Exists.html?good=true
Port = 80
Query = ?good=true
Scheme = http
Segments = /,Test/,Exists.html
UserEscaped = False
UserInfo =
----------------------------------------
However: The reverse is not true, as this example shows. It uses Console.WriteLine to print the results to the screen.
C# program that uses IsBaseOf method
using System;
class Program
{
static void Main()
{
// Two example Uri instances.
Uri uri1 = new Uri("http://dotnetCodex.com/");
Uri uri2 = new Uri("http://dotnetCodex.com/test/");
if (uri1.IsBaseOf(uri2))
{
Console.WriteLine(true); // Written.
}
if (uri2.IsBaseOf(uri1))
{
Console.WriteLine(false); // Not written.
}
}
}
Output
True
Tip: The UriPartial.Authority, Path, and Query arguments reveal longer parts of the left of the URI.
C# program that uses GetLeftPart method
using System;
class Program
{
static void Main()
{
// An example Uri instance.
Uri uri = new Uri("http://dotnetCodex.com/test/?cat=1");
// Test the GetLeftPart method.
Console.WriteLine(uri.GetLeftPart(UriPartial.Authority));
Console.WriteLine(uri.GetLeftPart(UriPartial.Path));
Console.WriteLine(uri.GetLeftPart(UriPartial.Query));
Console.WriteLine(uri.GetLeftPart(UriPartial.Scheme));
}
}
Output
http://dotnetCodex.com
http://dotnetCodex.com/test/
http://dotnetCodex.com/test/?cat=1
http://
Here: We determine a minimal, relative URI between two absolute URIs. We use the MakeRelativeUri method on the Uri instance.
C# program that uses MakeRelativeUri method
using System;
class Program
{
static void Main()
{
// Two absolute Uri instances.
Uri uri1 = new Uri("http://dotnetCodex.com/");
Uri uri2 = new Uri("http://dotnetCodex.com/Sort/Test.html");
// Get relative paths between them in both directions.
Console.WriteLine("uri2 to uri1 = {0}", uri2.MakeRelativeUri(uri1));
Console.WriteLine("uri1 to uri2 = {0}", uri1.MakeRelativeUri(uri2));
}
}
Output
uri2 to uri1 = ../
uri1 to uri2 = Sort/Test.html
Tip: The UriScheme* properties are what you would expect to see. They do not include any trailing delimiters.
Note: Some of these schemes, such as Gopher, are obsolete. I would be shocked if you ever needed to use them.
C# program that prints scheme string properties
using System;
class Program
{
static void Main()
{
// Print out the scheme delimiter.
// ... Then print out the various schemes.
Console.WriteLine(Uri.SchemeDelimiter);
Console.WriteLine(Uri.UriSchemeFile);
Console.WriteLine(Uri.UriSchemeFtp);
Console.WriteLine(Uri.UriSchemeGopher);
Console.WriteLine(Uri.UriSchemeHttp);
Console.WriteLine(Uri.UriSchemeHttps);
Console.WriteLine(Uri.UriSchemeMailto);
Console.WriteLine(Uri.UriSchemeNetPipe);
Console.WriteLine(Uri.UriSchemeNetTcp);
Console.WriteLine(Uri.UriSchemeNews);
Console.WriteLine(Uri.UriSchemeNntp);
}
}
Output
://
file
ftp
gopher
http
https
mailto
net.pipe
net.tcp
news
nntp
Note: They will not do any further validation than that. The scheme name "doesnotexist" will be considered a valid scheme name.
C# program that uses CheckHostName and CheckSchemeName
using System;
class Program
{
static void Main()
{
// Use CheckHostName and CheckSchemeName methods.
Console.WriteLine(Uri.CheckHostName("dotnetCodex.com"));
Console.WriteLine(Uri.CheckSchemeName("http"));
Console.WriteLine(Uri.CheckSchemeName(":)"));
}
}
Output
Dns
True
False
Caution: If you specify UriKind.Relative or RelativeOrAbsolute, you will often get a valid URI even if the syntax is strange.
Note: Relative URIs can simply be file names and not include domains or schemes.
C# program that uses TryCreate method
using System;
class Program
{
static void Main()
{
Uri uri1;
if (Uri.TryCreate("http://dotnetCodex.com/", UriKind.Absolute, out uri1))
{
Console.WriteLine("1 = {0}", uri1); // Reached.
}
Uri uri2;
if (Uri.TryCreate("http:dotnetCodex-com", UriKind.Absolute, out uri2))
{
Console.WriteLine("2 = {0}", uri2); // Not reached.
}
}
}
Output
1 = http://dotnetCodex.com/
And: For example, the escaped hexadecimal value "%20" represents the single space character.
Here: We demonstrate the FromHex, HexEscape, and IsHexDigit methods. There are several more on the Uri type as well.
C# program that uses Hex methods
using System;
class Program
{
static void Main()
{
// Use hex methods.
int value = Uri.FromHex('A');
string escape = Uri.HexEscape(' ');
bool hex = Uri.IsHexDigit('A');
// Print.
Console.WriteLine(value);
Console.WriteLine(escape);
Console.WriteLine(hex);
}
}
Output
10
%20
True
Note: The IsWellFormedUriString method returns a bool value that indicates if the string argument represents a valid URI.
C# program that uses IsWellFormedUriString method
using System;
class Program
{
static void Main()
{
// Use IsWellFormedUriString method.
bool a = Uri.IsWellFormedUriString("http://dotnetCodex.com/test", UriKind.Absolute);
bool b = Uri.IsWellFormedUriString("http:testcom-net", UriKind.Absolute);
// Display bools.
Console.WriteLine(a);
Console.WriteLine(b);
}
}
Output
True
False
Example: In the first instance, we pass 2 arguments to the constructor. These are received as the schemeName and the hostName.
Next: We compose a Uri by setting the Host, Path and Scheme properties directly. Finally, we show how to convert a UriBuilder to a Uri.
C# program that uses UriBuilder
using System;
class Program
{
static void Main()
{
// Use UriBuilder constructor.
UriBuilder u1 = new UriBuilder("http", "www.dotnetCodex.com");
Console.WriteLine(u1.ToString());
// Use UriBuilder properties.
UriBuilder u2 = new UriBuilder();
u2.Host = "www.dotnetCodex.com";
u2.Path = "uribuilder";
u2.Scheme = "http"; // Same as "http://"
Console.WriteLine(u2.ToString());
// Convert to Uri.
Uri uri = u2.Uri;
}
}
Output
http://www.dotnetCodex.com/
http://www.dotnetCodex.com/uribuilder
Tip: You do not need to worry about the punctuation characters here. The UriBuilder will resolve many patterns.