Uri.GetLeftPart(UriPartial) 方法

定义

获取实例的 Uri 指定部分。

public:
 System::String ^ GetLeftPart(UriPartial part);
public string GetLeftPart(UriPartial part);
member this.GetLeftPart : UriPartial -> string
Public Function GetLeftPart (part As UriPartial) As String

参数

part
UriPartial

指定要返回的 URI 部分末尾的枚举值之一。

返回

实例的 Uri 指定部分。

例外

当前 Uri 实例不是绝对实例。

指定的 part 值无效。

示例

以下示例创建一个 Uri 实例,并将路径写入控制台。

// Create Uri
Uri uriAddress = new Uri("http://www.contoso.com/index.htm#search");
Console.WriteLine(uriAddress.Fragment);
Console.WriteLine("Uri {0} the default port ", uriAddress.IsDefaultPort ? "uses" : "does not use");

Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Path));
Console.WriteLine("Hash code {0}", uriAddress.GetHashCode());
// The example displays output similar to the following:
//        #search
//        Uri uses the default port
//        The path of this Uri is http://www.contoso.com/index.htm
//        Hash code -988419291
// Create Uri
let uriAddress = Uri "http://www.contoso.com/index.htm#search"
printfn $"{uriAddress.Fragment}"
printfn $"""Uri {if uriAddress.IsDefaultPort then "uses" else "does not use"} the default port """

printfn $"The path of this Uri is {uriAddress.GetLeftPart UriPartial.Path}"
printfn $"Hash code {uriAddress.GetHashCode()}"
// The example displays output similar to the following:
//        #search
//        Uri uses the default port
//        The path of this Uri is http://www.contoso.com/index.htm
//        Hash code -988419291
' Create Uri
Dim uriAddress As New Uri("http://www.contoso.com/index.htm#search")
Console.WriteLine(uriAddress.Fragment)
Console.WriteLine("Uri {0} the default port ", If(uriAddress.IsDefaultPort, "uses", "does not use")) 

Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Path))
Console.WriteLine("Hash code {0}", uriAddress.GetHashCode())
' The example displays output similar to the following:
'        #search
'        Uri uses the default port
'        The path of this Uri is http://www.contoso.com/index.htm
'        Hash code -988419291

注解

该方法 GetLeftPart 返回一个字符串,其中包含 URI 字符串的最左侧部分,以指定的 part部分结尾。

Important

该方法 GetLeftPart 在其处理过程中执行 Unicode 字符编码和规范化。 这不是简单的字符串操作方法。 由于此编码行为,返回的字符串可能与原始 URI 字符串不同。

该方法GetLeftPart等效于使用相应的GetComponents标志进行调用UriComponents。 例如:

  • GetLeftPart(UriPartial.Authority)GetComponents(UriComponents.Scheme | UriComponents.UserInfo | UriComponents.Host | UriComponents.Port, UriFormat.UriEscaped) 等效

GetLeftPart 在以下情况下包括分隔符:

  • Scheme 包括方案分隔符。
  • Authority 不包括路径分隔符。
  • Path 包括原始 URI 中到查询或片段分隔符的路径分隔符和任何分隔符。
  • Query Path包括查询及其分隔符。

以下示例演示了 URI 以及使用 GetLeftPartSchemeAuthorityPath调用Query的结果。

URI Scheme 权威 路径 查询
http://www.contoso.com/index.htm?date=today http:// http://www.contoso.com http://www.contoso.com/index.htm http://www.contoso.com/index.htm?date=today
http://www.contoso.com/index.htm#main http:// http://www.contoso.com http://www.contoso.com/index.htm http://www.contoso.com/index.htm
mailto:user@contoso.com?subject=uri mailto: <none> mailto:user@contoso.com mailto:user@contoso.com?subject=uri
nntp://news.contoso.com/123456@contoso.com nntp:// nntp://news.contoso.com nntp://news.contoso.com/123456@contoso.com nntp://news.contoso.com/123456@contoso.com
news:123456@contoso.com news: <none> news:123456@contoso.com news:123456@contoso.com
file://server/filename.ext file:// file://server file://server/filename.ext file://server/filename.ext

适用于