Uri 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义一个对象,该对象表示统一资源标识符 (URI) 值并将其分析为组件。 Uri 对象由许多其他Windows 运行时 API 使用,这些 API 不一定局限于 Web 浏览器方案。
.NET 使用 .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
- 继承
- 属性
- 实现
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,该 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 的成员不同,本主题中所述的一些基本行为有所不同。 有关编码差异和可用成员的详细信息,请参阅 System.Uri (特别是备注) 。
下面是示例统一资源标识符 (URI) 部分的细分:
scheme://username:password@host:port/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
注意
username:password@ 在 主机 前面主要与文件传输协议 (FTP) 统一资源标识符 (URI) 相关。 文件传输协议 (FTP) URI 在 Windows 运行时 中受有限支持;请参阅“在后台传输数据” (HTML 或 XAML) 。
重要
并非所有可能的方案都可以由Windows 运行时应用使用。 值得注意的是,文件:方案被阻止。 可以创建一个 URI 对象,该对象使用Windows 运行时应用不支持的方案,在尝试将 Uri 作为 API 调用的输入值时,而不是在创建对象时被阻止。
特定于Windows 运行时应用的 URI 方案
Windows 运行时有多个方案,这些方案对Windows 运行时应用是唯一的,这些方案是指属于应用的资产。 通常,这些方案是可以使用的方案,而不是文件:方案,引用作为应用包的一部分包含的打包文件和其他资产,或者与已安装的应用关联的资产:
- ms-appx:引用应用的本地打包文件。
- ms-appx-web:还引用应用的本地打包文件,但在 Web 上下文中加载这些文件。 如果你访问资产以供 WebView 使用,或者编写 HTML/JavaScript 应用并从 HTML 页面请求,则通常需要这样做。 有关详细信息 ,请参阅 URI 方案 或 如何引用内容 (HTML) 。
- ms-appdata:指来自应用的本地、漫游和临时数据文件夹的应用文件。 有关详细信息 ,请参阅 URI 方案 或 存储和检索设置和其他应用数据。
- ms-resource:指 资源管理系统中的应用资源。 有关详细信息,请参阅对 UI 和应用包清单中的字符串进行本地化。
其中每个方案都会忽略统一资源标识符 (URI) 的许多组件部分,这些组件部件适用于其他方案 (,例如 查询 和 片段) 。 此外,某些组件被显式禁止,并导致使用这些方案的 Uri 值被视为无效的输入 (,例如 ms-appx 中的 UserName 和 Password 值:Uri 方案将使 Uri) 无效。 有关详细信息,请参阅 URI 方案。
在大多数情况下,将这些方案与三个斜杠一起使用, (示例:ms-appx:///page.html) ,该斜杠引用当前应用的根,使用当前应用作为颁发机构。 可以使用两个斜杠的用法,但可能需要显式指定颁发机构名称。 具有两个斜杠的用法可以引用包依赖项作为颁发机构,也可以引用应用中的相对位置。 有关详细信息,请参阅 URI 方案。
绝对 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 |
获取整个非规范 URI (它是非规范 URI,因为它实际上可能是 Windows.Foundation.Uri 编码行为的 IRI;请参阅 Remarks.) 。 |
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) | |
EscapeComponent(String) |
将统一资源标识符 (URI) 字符串转换为其转义表示形式。 |
ToString() |
获取当前 URI 的规范字符串表示形式。 |
UnescapeComponent(String) |
转换指定的字符串,方法是将任何转义序列替换为相应的未转义表示形式。 |
适用于
另请参阅
反馈
提交和查看相关反馈