Uri 類別

定義

定義 物件,此物件表示統一資源識別元 (URI) 值,並將其剖析為元件。 Uri 物件是由許多其他Windows 執行階段 API 使用,這些 API 不一定僅限於網頁瀏覽器案例。

。網 使用 .NET 進行程式設計時,會隱藏此類型,開發人員應該使用 System.Uri。 請參閱<備註>。

public ref class Uri sealed : IStringable
/// [Windows.Foundation.Metadata.Activatable(Windows.Foundation.IUriRuntimeClassFactory, 65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Uri final : IStringable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(Windows.Foundation.IUriRuntimeClassFactory, 65536, "Windows.Foundation.UniversalApiContract")]
class Uri final : IStringable
[Windows.Foundation.Metadata.Activatable(typeof(Windows.Foundation.IUriRuntimeClassFactory), 65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class Uri : IStringable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.Foundation.IUriRuntimeClassFactory), 65536, "Windows.Foundation.UniversalApiContract")]
public sealed class Uri : IStringable
function Uri(baseUri, relativeUri)
Public NotInheritable Class Uri
Implements IStringable
繼承
Object Platform::Object IInspectable Uri
屬性
實作

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

本範例會建立 Uri 物件,此物件代表使用 HTTP 通訊協定存取網站的統一資源識別項 (URI) 。

// The URI string
var uriToLaunch = "http://www.bing.com";

// Create a Uri object from the URI string
var uri = new Windows.Foundation.Uri(uriToLaunch);
// The URI string.
std::wstring uriToLaunch{ L"http://www.bing.com" };

// Create a Uri object from the URI string.
Windows::Foundation::Uri uri{ uriToLaunch };
// The URI string
var uriToLaunch = L"http://www.bing.com";

// Create a Uri object from the URI string
auto uri = ref new Windows::Foundation::Uri(uriToLaunch);

此範例會建立 URI,該 URI 會使用 ms-appx 通訊協定來存取應用程式套件中的影像檔。

// The URI string
var uriImage = "ms-appx:///images/SecondaryTileDefault-sdk.png";

// Create a Uri object from the URI string
var uri = new Windows.Foundation.Uri(uriImage);
// The URI string.
std::wstring uriImage{ L"ms-appx:///images/SecondaryTileDefault-sdk.png" };

// Create a Uri object from the URI string.
Windows::Foundation::Uri uri{ uriImage };
// The URI string
var uriImage = ;

// Create a Uri object from the URI string
auto uri = ref new Windows::Foundation::Uri(uriImage);

此範例會建立 URI,其會使用 ms-appdata 通訊協定來存取您應用程式的本機應用程式資料存放區中的檔案。

// The URI string
var uriFile = "ms-appdata:///local/file.ext";

// Create a Uri object from the URI string
var uri = new Windows.Foundation.Uri(uriFile);
// The URI string.
std::wstring uriFile{ L"ms-appdata:///local/file.ext" };

// Create a Uri object from the URI string.
Windows::Foundation::Uri uri{ uriFile };
// The URI string
var uriFile = "ms-appdata:///local/file.ext";

// Create a Uri object from the URI string
auto uri = ref new Windows::Foundation::Uri(uriFile);

備註

重要

使用 .NET 進行程式設計時,這個類別會隱藏,開發人員應該使用 System.Uri 類別,其會使用 RFC 3987 規則來編碼和解碼統一資源識別項 (URI) 。 Windows.Foundation.Uri 類別不會在統一資源識別項 (URI) 中以百分比編碼的非 ASCII 字元,其中配置參考 windows 檔案路徑 (,例如 ms-appx:) 。 Windows.Foundation.Uri 也會使用使用者的目前字碼頁來解譯百分比編碼。

System.Uri的可用成員與 Windows.Foundation.Uri 的成員類似,但與 Windows.Foundation.Uri 的成員不同,而本主題所述的一些基本行為則不同。 For more info on the encoding differences and what members are available, see System.Uri (particularly the Remarks).

以下是範例統一資源識別項 (URI) 的明細:

scheme://usernamepassword@hostport/path擴充功能查詢#片段

例如:

http://msdn.microsoft.com/library/windows/apps/windows.foundation.uri.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

在此範例中:

  • 配置http
  • 主機msdn.microsoft.com
  • 網域 (microsoft.com主機) 子集
  • 擴充 功能為 aspx
  • 查詢cs-save-lang=1&cs-lang=cpp
  • 片段code-snippet-1

注意

主機前面的 usernamepassword@ 主要與檔案傳輸通訊協定 (FTP) 統一資源識別項 (URI) 相關。 檔案傳輸通訊協定 (FTP) URI 在Windows 執行階段中具有有限的支援;請參閱 (HTMLXAML) 。

重要

並非所有可能的配置都可由Windows 執行階段應用程式使用。 值得注意的是,檔案:配置已遭到封鎖。 您可以建立 URI 物件,該物件會使用Windows 執行階段應用程式不支援的配置,而且當您嘗試將該 URI 套用為 API 呼叫的輸入值時,不會在建立物件時遭到封鎖。

Windows 執行階段應用程式專屬的 URI 配置

Windows 執行階段有數個Windows 執行階段應用程式特有的配置,而這些配置是指屬於應用程式一部分的資產。 一般而言,這些配置是您可以使用而非檔案的配置:配置,以參考已封裝的檔案和其他已包含在應用程式套件中的資產,或與已安裝應用程式相關聯的資產:

這些配置都會忽略統一資源識別項 (URI) (的許多元件元件部分,例如 查詢片段) 。 此外,明確不允許某些元件,並讓使用這些配置的 Uri 值被視為不正確輸入 (例如 ms-appx 中的 UserNamePassword 值:Uri 配置會使 Uri) 失效。 如需詳細資訊,請參閱 URI 配置

在大部分情況下,您會使用這些配置搭配三個斜線 (範例:ms-appx:///page.html) ,它會使用目前的應用程式作為授權單位參考目前應用程式的根目錄。 有兩個斜線的使用方式是可行的,但可能需要明確指定授權單位名稱。 具有兩個斜線的使用方式可以參考套件相依性作為授權單位,也可以參考應用程式內的相對位置。 如需詳細資訊,請參閱 URI 配置

絕對和相對 URI

在程式碼層級,Windows 執行階段不支援相對統一資源識別項 (URI) 。 您建立的所有 Uri 物件都必須代表絕對統一資源識別項 (URI) 。 上一節所列的配置實際上是絕對統一資源識別元 (URI) ,因為主機和授權單位會針對每個配置隱含,而路徑的其餘部分則會在該授權單位下進行評估。

依 URI 配置啟動應用程式

Uri 值的統一資源識別項 (URI) 配置可用來啟動與該特定配置相關聯的其他應用程式。 如需詳細資訊,請參閱 啟動 URI 的預設應用程式) 。

建構函式

Uri(String)

從指定的統一資源識別項 (URI) 字串, 初始化新的 URI 物件。 初始化 URI 也會剖析字串,並填入代表統一資源識別項的 Uri 屬性 (URI) 元件。

Uri(String, String)

藉由結合基底統一資源識別項 (URI) 和相對統一資源識別項 (URI) ,初始化新的 URI。 初始化 URI 也會剖析合併的字串,並填入代表統一資源識別項的 Uri 屬性 (URI) 元件。

屬性

AbsoluteCanonicalUri

取得目前 URI 的完整標準 RFC 相容標記法。

AbsoluteUri

根據 Windows.Foundation.Uri 編碼行為,取得整個非標準 URI (它是非標準 URI,因為它實際上可能是 IRI;請參閱備註.) 。

DisplayIri

取得組成目前 URI 的已解碼 Unicode 字元。

DisplayUri

取得可用於顯示用途的統一資源識別項 (URI) 標記法。

Domain

從統一資源識別項 (URI) 取得功能變數名稱元件,包括最上層網域。

Extension

取得 URI中所參考之資源的副檔名。

Fragment

取得片段標記之後的文字 (#) ,包括片段標記本身。

Host

取得完整功能變數名稱。

Password

取得統一資源識別項 (URI 的密碼元件,) 儲存在此 Uri 實例中。

Path

取得統一資源識別項 (URI 的路徑和資源名稱元件,) 儲存在此 Uri 實例中。

Port

取得統一資源識別項 (URI 的埠號碼元件,) 儲存在此 Uri 實例中

Query

取得統一資源識別項 (URI 的查詢字串元件,) 儲存在此 Uri 實例中。

QueryParsed

取得剖析的統一資源識別項 (URI) 查詢字串。

RawUri

取得整個原始統一資源識別元 (URI) 字串,以用來建構此 Uri 物件,再剖析,且未套用任何編碼。

SchemeName

取得統一資源識別項 (URI 的通訊協定配置名稱元件,) 儲存在此 Uri 實例中

Suspicious

取得值,這個值表示剖析是否判斷統一資源識別項 (URI) 格式不正確。

UserName

取得統一資源識別項 (URI 的使用者名稱元件,) 儲存在此 Uri 實例中。

方法

CombineUri(String)

將指定的統一資源識別項 (URI) 新增至目前的 URI

Equals(Uri)

判斷指定的 Uri 物件是否等於目前的 Uri 物件。

EscapeComponent(String)

將統一資源識別項 (URI) 字串轉換為其逸出表示。

ToString()

取得目前 URI的標準字串表示。

UnescapeComponent(String)

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

適用於

另請參閱