Uri Class
Namespace: System
Provides an object representation of a Uniform Resource Identifier (URI) and easy access to the parts of the URI.
Syntax
C#
public class Uri : MarshalByRefObject, System.Runtime.Serialization.ISerializable
Visual Basic
Public Class Uri
Inherits MarshalByRefObject
Implements ISerializable
Remarks
The
Uri
class represents a Uniform Resource Identifier (URI), which is an object that can exist independently of any specific protocol. A URI is a compact sequence of characters that identifies an abstract or physical resource. The Uri
class provides methods for parsing, manipulating, and validating URIs. It supports various URI schemes, including HTTP, HTTPS, FTP, and custom schemes.
Constructors
Uri(String uriString)
Initializes a new instance of the
Uri
class for the specified URI string.
Public Sub New(uriString As String)
C#
public Uri(string uriString);
Parameters
Name | Type | Description |
---|---|---|
uriString | System.String | A string that contains the URI to resolve. |
Uri(String uriString, Boolean dontEscape)
Initializes a new instance of the
Uri
class for the specified URI string and a value that indicates whether special URI characters are escaped.
Public Sub New(uriString As String, dontEscape As Boolean)
C#
public Uri(string uriString, bool dontEscape);
Parameters
Name | Type | Description |
---|---|---|
uriString | System.String | A string that contains the URI to resolve. |
dontEscape | System.Boolean | true to indicate that special URI characters in uriString should not be escaped; otherwise, false . |
Uri(Uri baseUri, String relativeUri)
Initializes a new instance of the
Uri
class for the specified relative URI and base URI.
Public Sub New(baseUri As Uri, relativeUri As String)
C#
public Uri(Uri baseUri, string relativeUri);
Parameters
Name | Type | Description |
---|---|---|
baseUri | System.Uri | The base URI for the relative URI. |
relativeUri | System.String | The relative URI to resolve. |
Properties
AbsoluteUri
Gets the absolute Uniform Resource Identifier (URI).
Public ReadOnly Property AbsoluteUri As Uri
C#
public Uri AbsoluteUri { get; }
IsAbsoluteUri
Gets a value that indicates whether the URI is absolute.
Public ReadOnly Property IsAbsoluteUri As Boolean
C#
public bool IsAbsoluteUri { get; }
Scheme
Gets the scheme name of the URI.
Public ReadOnly Property Scheme As String
C#
public string Scheme { get; }
Host
Gets the domain name of the URI.
Public ReadOnly Property Host As String
C#
public string Host { get; }
Port
Gets the port number of the URI.
Public ReadOnly Property Port As Integer
C#
public int Port { get; }
AbsolutePath
Gets the path component of the URI.
Public ReadOnly Property AbsolutePath As String
C#
public string AbsolutePath { get; }
Methods
IsBaseOf(Uri uri)
Determines whether the specified
Uri
is a base of the current Uri
.
Public Function IsBaseOf(uri As Uri) As Boolean
C#
public bool IsBaseOf(Uri uri);
Parameters
Name | Type | Description |
---|---|---|
uri | System.Uri | The Uri to compare with the current Uri . |
Returns
true
if the specified Uri
is a base of the current Uri
; otherwise, false
.
IsWellFormedUriString(String uriString, UriKind uriKind)
Checks whether the specified URI string is well-formed.
Public Shared Function IsWellFormedUriString(uriString As String, uriKind As UriKind) As Boolean
C#
public static bool IsWellFormedUriString(string uriString, UriKind uriKind);
Parameters
Name | Type | Description |
---|---|---|
uriString | System.String | The URI string to check. |
uriKind | System.UriKind | A UriKind enumeration that specifies whether to check for a relative or absolute URI. |
Returns
true
if uriString
is a well-formed URI of the specified type; otherwise, false
.
TryCreate(String uriString, UriKind uriKind, out Uri result)
Attempts to create a new
Uri
object from the specified string.
Public Shared Function TryCreate(uriString As String, uriKind As UriKind, ByRef result As Uri) As Boolean
C#
public static bool TryCreate(string uriString, UriKind uriKind, out Uri result);
Parameters
Name | Type | Description |
---|---|---|
uriString | System.String | The URI string to parse. |
uriKind | System.UriKind | A UriKind enumeration that specifies whether the URI is absolute or relative. |
result | out System.Uri | When this method returns, contains a Uri object representing the parsed URI, or null if parsing failed. |
Returns
true
if the uriString
was successfully parsed into a Uri
object; otherwise, false
.
Example
C# Example
using System;
public class Example
{
public static void Main()
{
string uriString = "https://www.example.com/path/to/resource?query=value#fragment";
Uri myUri = new Uri(uriString);
Console.WriteLine($"Absolute URI: {myUri.AbsoluteUri}");
Console.WriteLine($"Scheme: {myUri.Scheme}");
Console.WriteLine($"Host: {myUri.Host}");
Console.WriteLine($"Port: {myUri.Port}");
Console.WriteLine($"Absolute Path: {myUri.AbsolutePath}");
Console.WriteLine($"Is Absolute: {myUri.IsAbsoluteUri}");
// Example of relative URI
Uri baseUri = new Uri("https://www.example.com/");
Uri relativeUri = new Uri(baseUri, "path/to/another");
Console.WriteLine($"Relative URI: {relativeUri.AbsoluteUri}");
// Check if a URI string is well-formed
string testUriString = "http://contoso.com";
if (Uri.IsWellFormedUriString(testUriString, UriKind.Absolute))
{
Console.WriteLine($"'{testUriString}' is a well-formed absolute URI.");
}
// Try to create a Uri
Uri parsedUri;
if (Uri.TryCreate("ftp://ftp.example.com", UriKind.Absolute, out parsedUri))
{
Console.WriteLine($"Successfully created Uri: {parsedUri.AbsoluteUri}");
}
}
}
Output
Absolute URI: https://www.example.com/path/to/resource?query=value#fragment
Scheme: https
Host: www.example.com
Port: 443
Absolute Path: /path/to/resource
Is Absolute: True
Relative URI: https://www.example.com/path/to/another
'http://contoso.com' is a well-formed absolute URI.
Successfully created Uri: ftp://ftp.example.com