System.Net

Url Class

Summary

Represents a Uniform Resource Identifier (URI) reference.

Remarks

The System.Uri class represents a Uniform Resource Identifier (URI) reference. URIs are used to identify resources on the internet or on local systems. This class provides methods for parsing, manipulating, and comparing URIs.

The Uri class supports both absolute and relative URIs. An absolute URI includes a scheme (e.g., "http", "ftp", "file") and a hierarchical part, while a relative URI specifies a path relative to a base URI.

Class Members

Properties

AbsoluteUri

Gets the absolute Uniform Resource Identifier (URI).

public Uri AbsoluteUri { get; }

IsAbsoluteUri

Gets a value indicating whether the Uniform Resource Identifier (URI) is absolute.

public bool IsAbsoluteUri { get; }

IsFile

Gets a value indicating whether the Uniform Resource Identifier (URI) is an absolute file URI.

public bool IsFile { get; }

IsLoopback

Gets a value indicating whether the Uniform Resource Identifier (URI) is an absolute loopback URI.

public bool IsLoopback { get; }

IsUnc

Gets a value indicating whether the Uniform Resource Identifier (URI) is a Uniform Naming Convention (UNC) path.

public bool IsUnc { get; }

OriginalString

Gets the Uniform Resource Identifier (URI) as a string.

public string OriginalString { get; }

Scheme

Gets the scheme of the Uniform Resource Identifier (URI).

public string Scheme { get; }

Methods

ToString()

Returns the Uniform Resource Identifier (URI) as a string.

public override string ToString()
// Example usage:
Uri myUri = new Uri("http://www.contoso.com/resource?id=1");
string uriString = myUri.ToString();

Equals(object uri)

Determines whether the specified object is equal to the current Uri object.

public override bool Equals(object uri)

GetLeftPart(UriPartial part)

Returns a specified component of the URI.

public Uri GetLeftPart(UriPartial part)

UriPartial enumeration members:

  • UriPartial.Authority
  • UriPartial.Fragment
  • UriPartial.Host
  • UriPartial.Path
  • UriPartial.Port
  • UriPartial.Query
  • UriPartial.Scheme

Constructors

Url(string uriString)

Initializes a new instance of the Uri class with the specified string.

public Uri(string uriString)
Uri myUri = new Uri("http://www.contoso.com");

Url(string uriString, bool dontEscape)

Initializes a new instance of the Uri class with the specified string and a value that indicates whether to escape the URI.

public Uri(string uriString, bool dontEscape)

Example Usage

// Creating an absolute URI Uri absoluteUri = new Uri("https://docs.microsoft.com/en-us/dotnet/api/system.uri"); Console.WriteLine($"Absolute URI: {absoluteUri.AbsoluteUri}"); Console.WriteLine($"Scheme: {absoluteUri.Scheme}"); Console.WriteLine($"Is Absolute: {absoluteUri.IsAbsoluteUri}"); // Creating a relative URI (requires a base URI to resolve) Uri baseUri = new Uri("https://docs.microsoft.com/"); Uri relativeUri = new Uri(baseUri, "/en-us/dotnet/api/system.uri"); Console.WriteLine($"Relative URI resolved: {relativeUri.AbsoluteUri}"); // Checking URI properties if (absoluteUri.IsFile) { Console.WriteLine("This is a file URI."); } else if (absoluteUri.IsLoopback) { Console.WriteLine("This is a loopback URI."); } else { Console.WriteLine("This is a network URI."); }