Uri 類別

定義

提供統一資源識別元 (URI) 的物件表示,以及對 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 : MarshalByRefObject, System::Runtime::Serialization::ISerializable
public class Uri
public class Uri : System.Runtime.Serialization.ISerializable
public class 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 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
Inherits MarshalByRefObject
Implements ISerializable
繼承
Uri
繼承
屬性
實作

範例

下列範例會建立 類別的實例, Uri 並用它來搭配 HttpClient執行 GET 要求。

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)

下列代碼段顯示 類別上各種屬性的範例值。

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

備註

如需此 API 的詳細資訊,請參閱 Uri 的補充 API 備註

建構函式

Uri(SerializationInfo, StreamingContext)
已淘汰.

初始化 Uri 類別的新執行個體,這個執行個體是來自 SerializationInfoStreamingContext 類別的指定執行個體。

Uri(String)

使用指定的 URI,初始化 Uri 類別的新執行個體。

Uri(String, Boolean)
已淘汰.
已淘汰.
已淘汰.

以明確的字元逸出控制方式,初始化具有指定 URI 的 Uri 類別的新執行個體。

Uri(String, UriCreationOptions)

使用指定的 URI 和其他 UriCreationOptions,初始化 類別的新實例Uri

Uri(String, UriKind)

使用指定的 URI,初始化 Uri 類別的新執行個體。 這個建構函式可讓您指定 URI 字串是相對 URI、絕對 URI 或是不定。

Uri(Uri, String)

根據指定的基底 URI 和相對 URI 字串,初始化 Uri 類別的新執行個體。

Uri(Uri, String, Boolean)
已淘汰.
已淘汰.
已淘汰.

以明確的字元逸出控制方式,根據指定的基底和相對 URI,初始化 Uri 類別的新執行個體。

Uri(Uri, Uri)

根據指定的基底 Uri 執行個體和相對 Uri 執行個體的組合,初始化 Uri 類別的新執行個體。

欄位

SchemeDelimiter

指定會將通訊協定配置和 URI 的位址部分分隔開來的字元。 此欄位為唯讀。

UriSchemeFile

指定 URI 為檔案的指標。 此欄位為唯讀。

UriSchemeFtp

指定 URI 要透過檔案傳輸通訊協定 (File Transfer Protocol,FTP) 存取。 此欄位為唯讀。

UriSchemeFtps

指定透過檔案傳輸通訊協定安全 (FTPS) 存取 URI。 此欄位是唯讀的。

UriSchemeGopher

指定 URI 要透過 Gopher 通訊協定存取。 此欄位為唯讀。

UriSchemeHttp

指定 URI 要透過超文字傳輸協定 (Hypertext Transfer Protocol,HTTP) 存取。 此欄位為唯讀。

UriSchemeHttps

指定 URI 要透過安全超文字傳輸協定 (Secure Hypertext Transfer Protocol,HTTPS) 存取。 此欄位為唯讀。

UriSchemeMailto

指定 URI 為電子郵件地址,並且要透過簡易郵件傳輸通訊協定 (SMTP) 存取。 此欄位為唯讀。

UriSchemeNetPipe

指定 URI 會透過 Windows Communication Foundation (WCF) 所用的 NetPipe 配置來存取。 此欄位為唯讀。

UriSchemeNetTcp

指定 URI 會透過 Windows Communication Foundation (WCF) 所用的 NetTcp 配置來存取。 此欄位為唯讀。

UriSchemeNews

指定 URI 為網際網路新聞群組,並且要透過 Network News Transport Protocol (NNTP) 存取。 此欄位為唯讀。

UriSchemeNntp

指定 URI 為網際網路新聞群組,並且要透過 Network News Transport Protocol (NNTP) 存取。 此欄位為唯讀。

UriSchemeSftp

指定透過 SSH 檔案傳輸通訊協定 (SFTP) 來存取 URI。 此欄位是唯讀的。

UriSchemeSsh

指定透過安全套接字殼層通訊協定存取 URI (SSH) 。 此欄位是唯讀的。

UriSchemeTelnet

指定 URI 是透過 Telnet 通訊協定來存取。 此欄位是唯讀的。

UriSchemeWs

指定透過 WebSocket 通訊協定存取 URI (WS) 。 此欄位是唯讀的。

UriSchemeWss

指定透過 WebSocket Secure 通訊協定存取 URI (WSS) 。 此欄位是唯讀的。

屬性

AbsolutePath

取得 URI 的絕對路徑。

AbsoluteUri

取得絕對 URI。

Authority

取得伺服器的網域名稱系統 (DNS) 主機名稱或 IP 位址,以及連接埠編號。

DnsSafeHost

取得在視需要取消逸出後,可放心用於 DNS 解析的主機名稱。

Fragment

取得逸出 URI 片段,如果不是空的,則包括前置 『#』 字元。

Host

取得這個執行個體的主機元件。

HostNameType

取得 URI 中所指定主機名稱的類型。

IdnHost

取得主機符合 RFC 3490 標準的國際網域名稱,並適當地使用 Punycode。 這個字串在視需要取消逸出後,可放心用於 DNS 解析。

IsAbsoluteUri

取得指出 Uri 執行個體是否為絕對的值。

IsDefaultPort

取得指出 URI 的連接埠值是否為此配置之預設值的值。

IsFile

取得指出所指定 Uri 是否為檔案 URI 的值。

IsLoopback

取得指出所指定 Uri 是否會參考本機主機的值。

IsUnc

取得指出所指定 Uri 是否為通用命名慣例 (UNC) 路徑的值。

LocalPath

取得檔案名稱的本機作業系統表示。

OriginalString

取得傳遞給 Uri 建構函式的原始 URI 字串。

PathAndQuery

取得以問號 (?) 分隔的 AbsolutePathQuery 屬性。

Port

取得這個 URI 的連接埠編號。

Query

取得指定 URI 中包含的任何查詢資訊,如果不是空的,則包括前置 『?』 字元。

Scheme

取得這個 URI 的配置名稱。

Segments

取得陣列,包含構成指定 URI 的路徑區段。

UserEscaped

取得指出 URI 字串在 Uri 執行個體建立之前是否已完全逸出的值。

UserInfo

取得與指定 URI 關聯的使用者名稱、密碼或其他使用者特定的資訊。

方法

Canonicalize()
已淘汰.
已淘汰.
已淘汰.

將內部儲存的 URI 轉換成標準格式。

CheckHostName(String)

判斷指定的主機名稱是否為有效的 DNS 名稱。

CheckSchemeName(String)

判斷指定的配置名稱是否有效。

CheckSecurity()
已淘汰.
已淘汰.
已淘汰.

呼叫這個方法沒有任何作用。

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

使用指定的比較規則,比較兩個 URI 的指定部分。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Equals(Object)

比較兩個 Uri 執行個體是否相等。

Escape()
已淘汰.
已淘汰.
已淘汰.

將路徑元件中的任何不安全或保留字元轉換成十六進位字元表示。

EscapeDataString(ReadOnlySpan<Char>)

提供統一資源識別元 (URI) 的物件表示,以及對 URI 各部分的簡易存取。

EscapeDataString(String)

將字串轉換成它的逸出表示。

EscapeString(String)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

將字串轉換成它的逸出表示。

EscapeUriString(String)
已淘汰.
已淘汰.

將 URI 字串轉換成它的逸出表示。

FromHex(Char)

取得十六進位數字的十進位值。

GetComponents(UriComponents, UriFormat)

針對特殊字元使用指定的逸出方式,取得目前執行個體的指定元件。

GetHashCode()

取得 URI 的雜湊碼。

GetLeftPart(UriPartial)

取得 Uri 執行個體的指定部分。

GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)

傳回序列化目前執行個體所需的資料。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
HexEscape(Char)

將指定的字元轉換成其對等的十六進位數字。

HexUnescape(String, Int32)

將字元的指定十六進位表示轉換成字元。

InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
IsBadFileSystemCharacter(Char)
已淘汰.
已淘汰.
已淘汰.

指出字元在檔案系統名稱中是否無效。

IsBaseOf(Uri)

判斷目前 Uri 執行個體是否為指定之 Uri 執行個體的基底。

IsExcludedCharacter(Char)
已淘汰.
已淘汰.
已淘汰.

決定判斷是否應該逸出指定的字元。

IsHexDigit(Char)

判斷指定的字元是否為有效的十六進位數字。

IsHexEncoding(String, Int32)

判斷字串中的字元是否為十六進位編碼。

IsReservedCharacter(Char)
已淘汰.
已淘汰.
已淘汰.

決定指定的字元是否為保留字元。

IsWellFormedOriginalString()

指出用以建構此 Uri 之字串的語式是否正確,而且不需要進一步逸出。

IsWellFormedUriString(String, UriKind)

透過嘗試建構包含此字串的 URI 來表示字串的格式是否正確,且確保該字串不需要進一步逸出。

MakeRelative(Uri)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

判斷兩個 Uri 執行個體之間的差別。

MakeRelativeUri(Uri)

判斷兩個 Uri 執行個體之間的差別。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
Parse()
已淘汰.
已淘汰.
已淘汰.

剖析目前執行個體的 URI,確定它包含有效 URI 所需的所有部分。

ToString()

取得指定之 Uri 執行個體的標準字串表示。

TryCreate(String, UriCreationOptions, Uri)

使用指定的 String 實體與UriCreationOptions建立新的 Uri

TryCreate(String, UriKind, Uri)

使用指定的 Uri 執行個體和 String,建立新的 UriKind

TryCreate(Uri, String, Uri)

使用指定的基底和相對 Uri 執行個體,建立新的 String

TryCreate(Uri, Uri, Uri)

使用指定的基底和相對 Uri 執行個體,建立新的 Uri

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

提供統一資源識別元 (URI) 的物件表示,以及對 URI 各部分的簡易存取。

TryFormat(Span<Char>, Int32)

嘗試將 實例的標準字串表示 Uri 格式化為指定的範圍。

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

提供統一資源識別元 (URI) 的物件表示,以及對 URI 各部分的簡易存取。

Unescape(String)
已淘汰.
已淘汰.
已淘汰.

轉換指定的字串,方法是將任何逸出序列取代成未逸出的表示。

UnescapeDataString(ReadOnlySpan<Char>)

提供統一資源識別元 (URI) 的物件表示,以及對 URI 各部分的簡易存取。

UnescapeDataString(String)

將字串轉換成它的未逸出表示。

運算子

Equality(Uri, Uri)

判斷兩個 Uri 執行個體的值是否相同。

Inequality(Uri, Uri)

判斷兩個 Uri 執行個體的值是否不同。

明確介面實作

IFormattable.ToString(String, IFormatProvider)

使用指定的格式,格式化目前執行個體的值。

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

傳回序列化目前執行個體所需的資料。

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

嘗試將目前實例的值格式化為提供的字元範圍。

適用於

執行緒安全性

的所有成員 Uri 都是安全線程,而且可以從多個線程同時使用。

另請參閱