Muokkaa

Jaa


Uri Class

Definition

Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.

public ref class Uri
public ref class Uri : System::Runtime::Serialization::ISerializable
public ref class Uri : ISpanFormattable, System::Runtime::Serialization::ISerializable
public ref class Uri : IEquatable<Uri ^>, ISpanFormattable, System::Runtime::Serialization::ISerializable
public ref class Uri : MarshalByRefObject, System::Runtime::Serialization::ISerializable
public class Uri
public class Uri : System.Runtime.Serialization.ISerializable
public class Uri : ISpanFormattable, System.Runtime.Serialization.ISerializable
public class Uri : IEquatable<Uri>, ISpanFormattable, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class Uri : MarshalByRefObject, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.ComponentModel.TypeConverter(typeof(System.UriTypeConverter))]
public class Uri : System.Runtime.Serialization.ISerializable
type Uri = class
type Uri = class
    interface ISerializable
type Uri = class
    interface ISpanFormattable
    interface IFormattable
    interface ISerializable
type Uri = class
    interface IFormattable
    interface ISpanFormattable
    interface IEquatable<Uri>
    interface ISerializable
[<System.Serializable>]
type Uri = class
    inherit MarshalByRefObject
    interface ISerializable
[<System.Serializable>]
[<System.ComponentModel.TypeConverter(typeof(System.UriTypeConverter))>]
type Uri = class
    interface ISerializable
Public Class Uri
Public Class Uri
Implements ISerializable
Public Class Uri
Implements ISerializable, ISpanFormattable
Public Class Uri
Implements IEquatable(Of Uri), ISerializable, ISpanFormattable
Public Class Uri
Inherits MarshalByRefObject
Implements ISerializable
Inheritance
Uri
Inheritance
Attributes
Implements

Examples

The following example creates an instance of the Uri class and uses it to perform a GET request with HttpClient.

Uri^ siteUri = gcnew Uri("http://www.contoso.com/");

// HttpClient lifecycle management best practices:
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
HttpClient^ client = gcnew HttpClient;
HttpRequestMessage^ request = gcnew HttpRequestMessage(HttpMethod::Get, siteUri);
HttpResponseMessage^ response = client->Send(request);
Uri siteUri = new Uri("http://www.contoso.com/");

// HttpClient lifecycle management best practices:
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, siteUri);
HttpResponseMessage response = client.Send(request);
let siteUri = Uri "http://www.contoso.com/"

// HttpClient lifecycle management best practices:
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
use client = new HttpClient ()
use request = new HttpRequestMessage (HttpMethod.Get, siteUri)
use response = client.Send request
Dim siteUri As New Uri("http://www.contoso.com/")

' HttpClient lifecycle management best practices:
' https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
Dim client As New HttpClient()
Dim request As New HttpRequestMessage(HttpMethod.Get, siteUri)
Dim response As HttpResponseMessage = client.Send(request)

The following code snippet shows example values of the various properties on the class.

Uri uri = new Uri("https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName");

Console.WriteLine($"AbsolutePath: {uri.AbsolutePath}");
Console.WriteLine($"AbsoluteUri: {uri.AbsoluteUri}");
Console.WriteLine($"DnsSafeHost: {uri.DnsSafeHost}");
Console.WriteLine($"Fragment: {uri.Fragment}");
Console.WriteLine($"Host: {uri.Host}");
Console.WriteLine($"HostNameType: {uri.HostNameType}");
Console.WriteLine($"IdnHost: {uri.IdnHost}");
Console.WriteLine($"IsAbsoluteUri: {uri.IsAbsoluteUri}");
Console.WriteLine($"IsDefaultPort: {uri.IsDefaultPort}");
Console.WriteLine($"IsFile: {uri.IsFile}");
Console.WriteLine($"IsLoopback: {uri.IsLoopback}");
Console.WriteLine($"IsUnc: {uri.IsUnc}");
Console.WriteLine($"LocalPath: {uri.LocalPath}");
Console.WriteLine($"OriginalString: {uri.OriginalString}");
Console.WriteLine($"PathAndQuery: {uri.PathAndQuery}");
Console.WriteLine($"Port: {uri.Port}");
Console.WriteLine($"Query: {uri.Query}");
Console.WriteLine($"Scheme: {uri.Scheme}");
Console.WriteLine($"Segments: {string.Join(", ", uri.Segments)}");
Console.WriteLine($"UserEscaped: {uri.UserEscaped}");
Console.WriteLine($"UserInfo: {uri.UserInfo}");

// AbsolutePath: /Home/Index.htm
// AbsoluteUri: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// DnsSafeHost: www.contoso.com
// Fragment: #FragmentName
// Host: www.contoso.com
// HostNameType: Dns
// IdnHost: www.contoso.com
// IsAbsoluteUri: True
// IsDefaultPort: False
// IsFile: False
// IsLoopback: False
// IsUnc: False
// LocalPath: /Home/Index.htm
// OriginalString: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// PathAndQuery: /Home/Index.htm?q1=v1&q2=v2
// Port: 80
// Query: ?q1=v1&q2=v2
// Scheme: https
// Segments: /, Home/, Index.htm
// UserEscaped: False
// UserInfo: user:password
let uri = Uri "https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName"

printfn $"AbsolutePath: {uri.AbsolutePath}"
printfn $"AbsoluteUri: {uri.AbsoluteUri}"
printfn $"DnsSafeHost: {uri.DnsSafeHost}"
printfn $"Fragment: {uri.Fragment}"
printfn $"Host: {uri.Host}"
printfn $"HostNameType: {uri.HostNameType}"
printfn $"IdnHost: {uri.IdnHost}"
printfn $"IsAbsoluteUri: {uri.IsAbsoluteUri}"
printfn $"IsDefaultPort: {uri.IsDefaultPort}"
printfn $"IsFile: {uri.IsFile}"
printfn $"IsLoopback: {uri.IsLoopback}"
printfn $"IsUnc: {uri.IsUnc}"
printfn $"LocalPath: {uri.LocalPath}"
printfn $"OriginalString: {uri.OriginalString}"
printfn $"PathAndQuery: {uri.PathAndQuery}"
printfn $"Port: {uri.Port}"
printfn $"Query: {uri.Query}"
printfn $"Scheme: {uri.Scheme}"
printfn $"""Segments: {String.Join(", ", uri.Segments)}"""
printfn $"UserEscaped: {uri.UserEscaped}"
printfn $"UserInfo: {uri.UserInfo}"

// AbsolutePath: /Home/Index.htm
// AbsoluteUri: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// DnsSafeHost: www.contoso.com
// Fragment: #FragmentName
// Host: www.contoso.com
// HostNameType: Dns
// IdnHost: www.contoso.com
// IsAbsoluteUri: True
// IsDefaultPort: False
// IsFile: False
// IsLoopback: False
// IsUnc: False
// LocalPath: /Home/Index.htm
// OriginalString: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// PathAndQuery: /Home/Index.htm?q1=v1&q2=v2
// Port: 80
// Query: ?q1=v1&q2=v2
// Scheme: https
// Segments: /, Home/, Index.htm
// UserEscaped: False
// UserInfo: user:password

Remarks

For more information about this API, see Supplemental API remarks for Uri.

Constructors

Uri(SerializationInfo, StreamingContext)
Obsolete.

Initializes a new instance of the Uri class from the specified instances of the SerializationInfo and StreamingContext classes.

Uri(String, Boolean)
Obsolete.
Obsolete.
Obsolete.

Initializes a new instance of the Uri class with the specified URI, with explicit control of character escaping.

Uri(String, UriCreationOptions)

Initializes a new instance of the Uri class with the specified URI and additional UriCreationOptions.

Uri(String, UriKind)

Initializes a new instance of the Uri class with the specified URI. This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate.

Uri(String)

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

Uri(Uri, String, Boolean)
Obsolete.
Obsolete.
Obsolete.

Initializes a new instance of the Uri class based on the specified base and relative URIs, with explicit control of character escaping.

Uri(Uri, String)

Initializes a new instance of the Uri class based on the specified base URI and relative URI string.

Uri(Uri, Uri)

Initializes a new instance of the Uri class based on the combination of a specified base Uri instance and a relative Uri instance.

Fields

SchemeDelimiter

Specifies the characters that separate the communication protocol scheme from the address portion of the URI. This field is read-only.

UriSchemeFile

Specifies that the URI is a pointer to a file. This field is read-only.

UriSchemeFtp

Specifies that the URI is accessed through the File Transfer Protocol (FTP). This field is read-only.

UriSchemeFtps

Specifies that the URI is accessed through the File Transfer Protocol Secure (FTPS). This field is read-only.

UriSchemeGopher

Specifies that the URI is accessed through the Gopher protocol. This field is read-only.

UriSchemeHttp

Specifies that the URI is accessed through the Hypertext Transfer Protocol (HTTP). This field is read-only.

UriSchemeHttps

Specifies that the URI is accessed through the Secure Hypertext Transfer Protocol (HTTPS). This field is read-only.

UriSchemeMailto

Specifies that the URI is an email address and is accessed through the Simple Mail Transport Protocol (SMTP). This field is read-only.

UriSchemeNetPipe

Specifies that the URI is accessed through the NetPipe scheme used by Windows Communication Foundation (WCF). This field is read-only.

UriSchemeNetTcp

Specifies that the URI is accessed through the NetTcp scheme used by Windows Communication Foundation (WCF). This field is read-only.

UriSchemeNews

Specifies that the URI is an Internet news group and is accessed through the Network News Transport Protocol (NNTP). This field is read-only.

UriSchemeNntp

Specifies that the URI is an Internet news group and is accessed through the Network News Transport Protocol (NNTP). This field is read-only.

UriSchemeSftp

Specifies that the URI is accessed through the SSH File Transfer Protocol (SFTP). This field is read-only.

UriSchemeSsh

Specifies that the URI is accessed through the Secure Socket Shell protocol (SSH). This field is read-only.

UriSchemeTelnet

Specifies that the URI is accessed through the Telnet protocol. This field is read-only.

UriSchemeWs

Specifies that the URI is accessed through the WebSocket protocol (WS). This field is read-only.

UriSchemeWss

Specifies that the URI is accessed through the WebSocket Secure protocol (WSS). This field is read-only.

Properties

AbsolutePath

Gets the absolute path of the URI.

AbsoluteUri

Gets the absolute URI.

Authority

Gets the Domain Name System (DNS) host name or IP address and the port number for a server.

DnsSafeHost

Gets a host name that, after being unescaped if necessary, is safe to use for DNS resolution.

Fragment

Gets the escaped URI fragment, including the leading '#' character if not empty.

Host

Gets the host component of this instance.

HostNameType

Gets the type of the host name specified in the URI.

IdnHost

Gets the RFC 3490 compliant International Domain Name of the host, using Punycode as appropriate. This string, after being unescaped if necessary, is safe to use for DNS resolution.

IsAbsoluteUri

Gets a value that indicates whether the Uri instance is absolute.

IsDefaultPort

Gets a value that indicates whether the port value of the URI is the default for this scheme.

IsFile

Gets a value that indicates whether the specified Uri is a file URI.

IsLoopback

Gets a value that indicates whether the specified Uri references the local host.

IsUnc

Gets a value that indicates whether the specified Uri is a universal naming convention (UNC) path.

LocalPath

Gets a local operating-system representation of a file name.

OriginalString

Gets the original URI string that was passed to the Uri constructor.

PathAndQuery

Gets the AbsolutePath and Query properties separated by a question mark (?).

Port

Gets the port number of this URI.

Query

Gets any query information included in the specified URI, including the leading '?' character if not empty.

Scheme

Gets the scheme name for this URI.

Segments

Gets an array containing the path segments that make up the specified URI.

UserEscaped

Gets a value that indicates whether the URI string was completely escaped before the Uri instance was created.

UserInfo

Gets the user name, password, or other user-specific information associated with the specified URI.

Methods

Canonicalize()
Obsolete.
Obsolete.
Obsolete.

Converts the internally stored URI to canonical form.

CheckHostName(String)

Determines whether the specified host name is a valid DNS name.

CheckSchemeName(String)

Determines whether the specified scheme name is valid.

CheckSecurity()
Obsolete.
Obsolete.
Obsolete.

Calling this method has no effect.

Compare(Uri, Uri, UriComponents, UriFormat, StringComparison)

Compares the specified parts of two URIs using the specified comparison rules.

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Equals(Object)

Compares two Uri instances for equality.

Equals(Uri)

Compares two Uri instances for equality.

Escape()
Obsolete.
Obsolete.
Obsolete.

Converts any unsafe or reserved characters in the path component to their hexadecimal character representations.

EscapeDataString(ReadOnlySpan<Char>)

Converts a span to its escaped representation.

EscapeDataString(String)

Converts a string to its escaped representation.

EscapeString(String)
Obsolete.
Obsolete.
Obsolete.
Obsolete.

Converts a string to its escaped representation.

EscapeUriString(String)
Obsolete.
Obsolete.

Converts a URI string to its escaped representation.

FromHex(Char)

Gets the decimal value of a hexadecimal digit.

GetComponents(UriComponents, UriFormat)

Gets the specified components of the current instance using the specified escaping for special characters.

GetHashCode()

Gets the hash code for the URI.

GetLeftPart(UriPartial)

Gets the specified portion of a Uri instance.

GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)

Returns the data needed to serialize the current instance.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
HexEscape(Char)

Converts a specified character into its hexadecimal equivalent.

HexUnescape(String, Int32)

Converts a specified hexadecimal representation of a character to the character.

InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
IsBadFileSystemCharacter(Char)
Obsolete.
Obsolete.
Obsolete.

Indicates whether a character is invalid in a file system name.

IsBaseOf(Uri)

Determines whether the current Uri instance is a base of the specified Uri instance.

IsExcludedCharacter(Char)
Obsolete.
Obsolete.
Obsolete.

Determines whether the specified character should be escaped.

IsHexDigit(Char)

Determines whether a specified character is a valid hexadecimal digit.

IsHexEncoding(String, Int32)

Determines whether a character in a string is hexadecimal encoded.

IsReservedCharacter(Char)
Obsolete.
Obsolete.
Obsolete.

Determines whether the specified character is a reserved character.

IsWellFormedOriginalString()

Indicates whether the string used to construct this Uri was well-formed and does not require further escaping.

IsWellFormedUriString(String, UriKind)

Indicates whether the string is well-formed by attempting to construct a URI with the string and ensures that the string does not require further escaping.

MakeRelative(Uri)
Obsolete.
Obsolete.
Obsolete.
Obsolete.

Determines the difference between two Uri instances.

MakeRelativeUri(Uri)

Determines the difference between two Uri instances.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
Parse()
Obsolete.
Obsolete.
Obsolete.

Parses the URI of the current instance to ensure it contains all the parts required for a valid URI.

ToString()

Gets a canonical string representation for the specified Uri instance.

TryCreate(String, UriCreationOptions, Uri)

Creates a new Uri using the specified String instance and UriCreationOptions.

TryCreate(String, UriKind, Uri)

Creates a new Uri using the specified String instance and a UriKind.

TryCreate(Uri, String, Uri)

Creates a new Uri using the specified base and relative String instances.

TryCreate(Uri, Uri, Uri)

Creates a new Uri using the specified base and relative Uri instances.

TryEscapeDataString(ReadOnlySpan<Char>, Span<Char>, Int32)

Attempts to convert a span to its escaped representation.

TryFormat(Span<Char>, Int32)

Attempts to format a canonical string representation for the Uri instance into the specified span.

TryUnescapeDataString(ReadOnlySpan<Char>, Span<Char>, Int32)

Attempts to convert a span to its unescaped representation.

Unescape(String)
Obsolete.
Obsolete.
Obsolete.

Converts the specified string by replacing any escape sequences with their unescaped representation.

UnescapeDataString(ReadOnlySpan<Char>)

Converts a span to its unescaped representation.

UnescapeDataString(String)

Converts a string to its unescaped representation.

Operators

Equality(Uri, Uri)

Determines whether two Uri instances have the same value.

Inequality(Uri, Uri)

Determines whether two Uri instances do not have the same value.

Explicit Interface Implementations

IFormattable.ToString(String, IFormatProvider)

Formats the value of the current instance using the specified format.

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Returns the data needed to serialize the current instance.

ISpanFormattable.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

Tries to format the value of the current instance into the provided span of characters.

Applies to

Thread Safety

All members of Uri are thread-safe and may be used concurrently from multiple threads.

See also