Uri 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供統一資源識別元 (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 類別的新執行個體,這個執行個體是來自 SerializationInfo 和 StreamingContext 類別的指定執行個體。 |
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) |
欄位
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 |
取得以問號 (?) 分隔的 AbsolutePath 和 Query 屬性。 |
Port |
取得這個 URI 的連接埠編號。 |
Query |
取得指定 URI 中包含的任何查詢資訊,如果不是空的,則包括前置 『?』 字元。 |
Scheme |
取得這個 URI 的配置名稱。 |
Segments |
取得陣列,包含構成指定 URI 的路徑區段。 |
UserEscaped |
取得指出 URI 字串在 Uri 執行個體建立之前是否已完全逸出的值。 |
UserInfo |
取得與指定 URI 關聯的使用者名稱、密碼或其他使用者特定的資訊。 |
方法
運算子
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 都是安全線程,而且可以從多個線程同時使用。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應