Uri Uri Uri Uri Constructors

定义

重载

Uri(String) Uri(String) Uri(String) Uri(String)

用指定的 URI 初始化 Uri 类的新实例。Initializes a new instance of the Uri class with the specified URI.

Uri(SerializationInfo, StreamingContext) Uri(SerializationInfo, StreamingContext) Uri(SerializationInfo, StreamingContext)

UriSerializationInfo 类的指定实例初始化 StreamingContext 类的新实例。Initializes a new instance of the Uri class from the specified instances of the SerializationInfo and StreamingContext classes.

Uri(String, Boolean) Uri(String, Boolean) Uri(String, Boolean) Uri(String, Boolean)

用指定的 URI 初始化 Uri 类的新实例,并对字符转义进行显式控制。Initializes a new instance of the Uri class with the specified URI, with explicit control of character escaping.

Uri(String, UriKind) Uri(String, UriKind) Uri(String, UriKind)

用指定的 URI 初始化 Uri 类的新实例。Initializes a new instance of the Uri class with the specified URI. 此构造函数允许指定 URI 字符串是相对 URI、绝对 URI 还是不确定。This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate.

Uri(Uri, String) Uri(Uri, String) Uri(Uri, String) Uri(Uri, String)

根据指定的基 URI 和相对 URI 字符串,初始化 Uri 类的新实例。Initializes a new instance of the Uri class based on the specified base URI and relative URI string.

Uri(Uri, Uri) Uri(Uri, Uri) Uri(Uri, Uri) Uri(Uri, Uri)

根据指定的基 Uri 实例和相对 Uri 实例的组合,初始化 Uri 类的新实例。Initializes a new instance of the Uri class based on the combination of a specified base Uri instance and a relative Uri instance.

Uri(Uri, String, Boolean) Uri(Uri, String, Boolean) Uri(Uri, String, Boolean) Uri(Uri, String, Boolean)

根据指定的基 URI 和相对 URI,初始化 Uri 类的新实例,并对字符转义进行显式控制。Initializes a new instance of the Uri class based on the specified base and relative URIs, with explicit control of character escaping.

Uri(String) Uri(String) Uri(String) Uri(String)

用指定的 URI 初始化 Uri 类的新实例。Initializes a new instance of the Uri class with the specified URI.

public:
 Uri(System::String ^ uriString);
public Uri (string uriString);
new Uri : string -> Uri
Public Sub New (uriString As String)

参数

uriString
String String String String

标识将由 Uri 实例表示的资源的字符串。A string that identifies the resource to be represented by the Uri instance. 请注意,必须用括号将字符串形式的 IPv6 地址括起来。Note that an IPv6 address in string form must be enclosed within brackets. 例如,“http://[2607:f8b0:400d:c06::69]”。For example, "http://[2607:f8b0:400d:c06::69]".

异常

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 FormatExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

uriString 为空。 uriString is empty.

-or- uriString 中指定的方案格式不正确。The scheme specified in uriString is not correctly formed. 请参阅 CheckSchemeName(String)See CheckSchemeName(String).

- 或 --or- uriString 包含过多斜杠。uriString contains too many slashes.

-or- uriString 中指定的密码无效。The password specified in uriString is not valid.

- 或 --or- uriString 中指定的主机名无效。The host name specified in uriString is not valid.

-or- uriString 中指定的文件名无效。The file name specified in uriString is not valid.

-or- uriString 中指定的用户名无效。The user name specified in uriString is not valid.

- 或 --or- uriString 中指定的主机名或证书颁发机构名称不能以反斜杠结尾。The host or authority name specified in uriString cannot be terminated by backslashes.

-or- uriString 中指定的端口号无效或无法分析。The port number specified in uriString is not valid or cannot be parsed.

-or- uriString 的长度超过 65519 个字符。The length of uriString exceeds 65519 characters.

-or- uriString 中指定的方案的长度超过 1023 个字符。The length of the scheme specified in uriString exceeds 1023 characters.

- 或 --or- uriString 中存在无效的字符序列。There is an invalid character sequence in uriString.

-or- uriString 中指定的 MS-DOS 路径必须以 c:\\ 开头。The MS-DOS path specified in uriString must start with c:\\.

示例

下面的示例创建一个Uri具有 URI http://www.contoso.com/的实例。The following example creates a Uri instance with the URI http://www.contoso.com/.

Uri^ baseUri = gcnew Uri( "http://www.contoso.com/" );
Uri myUri = new Uri("http://www.contoso.com/");

Dim myUri As New Uri("http://www.contoso.com/")

注解

此构造函数从Uri URI 字符串创建实例。This constructor creates a Uri instance from a URI string. 它分析 URI,使其采用规范格式,并进行任何所需的转义编码。It parses the URI, puts it in canonical format, and makes any required escape encodings.

此构造函数不确保Uri引用辅助资源。This constructor does not ensure that the Uri refers to an accessible resource.

此构造string函数假设参数引用了一个绝对 URI,并且等效于在UriKind设置UriAbsolute的情况下调用构造函数。This constructor assumes that the string parameter references an absolute URI and is equivalent to calling the Uri constructor with UriKind set to Absolute. 如果传递给构造函数的UriFormatException参数是相对URI,则此构造函数将引发。stringIf the string parameter passed to the constructor is a relative URI, this constructor will throw a UriFormatException.

Uri(SerializationInfo, StreamingContext) Uri(SerializationInfo, StreamingContext) Uri(SerializationInfo, StreamingContext)

UriSerializationInfo 类的指定实例初始化 StreamingContext 类的新实例。Initializes a new instance of the Uri class from the specified instances of the SerializationInfo and StreamingContext classes.

protected:
 Uri(System::Runtime::Serialization::SerializationInfo ^ serializationInfo, System::Runtime::Serialization::StreamingContext streamingContext);
protected Uri (System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext);
new Uri : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> Uri

参数

serializationInfo
SerializationInfo SerializationInfo SerializationInfo SerializationInfo

SerializationInfo 类的实例,包含序列化新的 Uri 实例所需的信息。An instance of the SerializationInfo class containing the information required to serialize the new Uri instance.

streamingContext
StreamingContext StreamingContext StreamingContext StreamingContext

StreamingContext 类的实例,包含与新的 Uri 实例关联的序列化流的源。An instance of the StreamingContext class containing the source of the serialized stream associated with the new Uri instance.

异常

serializationInfo 参数包含 null URI。The serializationInfo parameter contains a null URI.

serializationInfo 参数包含一个为空的 URI。The serializationInfo parameter contains a URI that is empty.

-or- 指定的方案形式不正确。The scheme specified is not correctly formed. 请参阅 CheckSchemeName(String)See CheckSchemeName(String).

-or- 该 URI 包含过多斜杠。The URI contains too many slashes.

-or- URI 中指定的密码无效。The password specified in the URI is not valid.

-or- URI 中指定的主机名无效。The host name specified in URI is not valid.

- 或 --or- URI 中指定的文件名无效。The file name specified in the URI is not valid.

-or- URI 中指定的用户名无效。The user name specified in the URI is not valid.

- 或 --or- URI 中指定的主机名或证书颁发机构名称不能以反斜杠结尾。The host or authority name specified in the URI cannot be terminated by backslashes.

-or- URI 中指定的端口号无效或无法分析。The port number specified in the URI is not valid or cannot be parsed.

-or- URI 的长度超过 65519 个字符。The length of URI exceeds 65519 characters.

-or- URI 中指定的方案的长度超过 1023 个字符。The length of the scheme specified in the URI exceeds 1023 characters.

-or- URI 中存在无效的字符序列。There is an invalid character sequence in the URI.

-or- URI 中指定的 MS-DOS 路径必须以 c:\\ 开头。The MS-DOS path specified in the URI must start with c:\\.

注解

此构造函数实现ISerializable Uri类的接口。This constructor implements the ISerializable interface for the Uri class.

另请参阅

Uri(String, Boolean) Uri(String, Boolean) Uri(String, Boolean) Uri(String, Boolean)

警告

此 API 现已过时。

用指定的 URI 初始化 Uri 类的新实例,并对字符转义进行显式控制。Initializes a new instance of the Uri class with the specified URI, with explicit control of character escaping.

public:
 Uri(System::String ^ uriString, bool dontEscape);
[System.Obsolete("The constructor has been deprecated. Please use new Uri(string). The dontEscape parameter is deprecated and is always false. http://go.microsoft.com/fwlink/?linkid=14202")]
[System.Obsolete("The constructor has been deprecated. Please use new Uri(string). The dontEscape parameter is deprecated and is always false. https://go.microsoft.com/fwlink/?linkid=14202")]
public Uri (string uriString, bool dontEscape);
new Uri : string * bool -> Uri
Public Sub New (uriString As String, dontEscape As Boolean)

参数

uriString
String String String String

标识将由 Uri 实例表示的资源的字符串。A string that identifies the resource to be represented by the Uri instance. 请注意,必须用括号将字符串形式的 IPv6 地址括起来。Note that an IPv6 address in string form must be enclosed within brackets. 例如,“http://[2607:f8b0:400d:c06::69]”。For example, "http://[2607:f8b0:400d:c06::69]".

dontEscape
Boolean Boolean Boolean Boolean

如果 uriString 已完全转义,则为 true;否则为 falsetrue if uriString is completely escaped; otherwise, false.

异常

uriString 为空或只包含空格。uriString is empty or contains only spaces.

- 或 --or- uriString 中指定的方案无效。The scheme specified in uriString is not valid.

- 或 --or- uriString 包含过多斜杠。uriString contains too many slashes.

- 或 --or- uriString 中指定的密码无效。The password specified in uriString is not valid.

-or- uriString 中指定的主机名无效。The host name specified in uriString is not valid.

-or- uriString 中指定的文件名无效。The file name specified in uriString is not valid.

-or- uriString 中指定的用户名无效。The user name specified in uriString is not valid.

- 或 --or- uriString 中指定的主机名或证书颁发机构名称不能以反斜杠结尾。The host or authority name specified in uriString cannot be terminated by backslashes.

- 或 --or- uriString 中指定的端口号无效或无法分析。The port number specified in uriString is not valid or cannot be parsed.

-or- uriString 的长度超过 65519 个字符。The length of uriString exceeds 65519 characters.

-or- uriString 中指定的方案的长度超过 1023 个字符。The length of the scheme specified in uriString exceeds 1023 characters.

- 或 --or- uriString 中存在无效的字符序列。There is an invalid character sequence in uriString.

-or- uriString 中指定的 MS-DOS 路径必须以 c:\\ 开头。The MS-DOS path specified in uriString must start with c:\\.

示例

下面的示例创建Uri URI http://www.contoso.com/Hello%20World.htm的实例。The following example creates a Uri instance for the URI http://www.contoso.com/Hello%20World.htm. 由于包含的 URI 已完全转义并且采用规范格式, dontEscape因此可以将参数设置为。 trueBecause the contained URI is completely escaped and is in canonical form, the dontEscape parameter can be set to true.

Uri^ myUri = gcnew Uri(  "http://www.contoso.com/Hello%20World.htm",true );
Uri myUri = new Uri("http://www.contoso.com/Hello%20World.htm", true);

Dim myUri As New Uri("http://www.contoso.com/Hello%20World.htm", True)

注解

此构造函数从Uri URI 字符串创建实例。This constructor creates a Uri instance from a URI string. 它分析 URI 并将其采用规范格式。It parses the URI and puts it in canonical format.

dontEscape参数控制是否将保留字符转换为转义序列。The dontEscape parameter controls whether reserved characters are translated into escape sequences. 仅当你确定 URI 中true的所有保留字符都已转义时,才应将此参数设置为。This parameter should be set to true only if you are certain that all reserved characters in the URI have been escaped. 对于未完全转义true的 URI,将值设置为会导致意外的行为。Setting the value to true for a URI that has not been completely escaped can cause unexpected behavior. 强烈建议您始终将此参数设置为falseIt is strongly recommended that you always set this parameter to false.

如果dontEscape设置为false,则构造函数将通过检查是否存在百分比(%)来转义任何保留字符后跟有效的转义序列。If dontEscape is set to false, the constructor escapes any reserved characters by checking that all occurrences of percent (%) are followed by a valid escape sequence. 如果百分号后面的字符序列无效,则百分比将替换为% 25。If the character sequence following a percent is not valid, the percent is replaced by %25.

此构造函数不确保Uri引用辅助资源。This constructor does not ensure that the Uri refers to an accessible resource.

Uri(String, UriKind) Uri(String, UriKind) Uri(String, UriKind)

用指定的 URI 初始化 Uri 类的新实例。Initializes a new instance of the Uri class with the specified URI. 此构造函数允许指定 URI 字符串是相对 URI、绝对 URI 还是不确定。This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate.

public:
 Uri(System::String ^ uriString, UriKind uriKind);
public Uri (string uriString, UriKind uriKind);
new Uri : string * UriKind -> Uri

参数

uriString
String String String String

标识将由 Uri 实例表示的资源的字符串。A string that identifies the resource to be represented by the Uri instance. 请注意,必须用括号将字符串形式的 IPv6 地址括起来。Note that an IPv6 address in string form must be enclosed within brackets. 例如,“http://[2607:f8b0:400d:c06::69]”。For example, "http://[2607:f8b0:400d:c06::69]".

uriKind
UriKind UriKind UriKind UriKind

指定 URI 字符串是相对 URI、绝对 URI 还是不确定。Specifies whether the URI string is a relative URI, absolute URI, or is indeterminate.

异常

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 FormatExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

uriString 包含相对 URI,而 uriKindAbsolute uriString contains a relative URI and uriKind is Absolute.

oror uriString 包含绝对 URI,而 uriKindRelativeuriString contains an absolute URI and uriKind is Relative.

oror uriString 为空。uriString is empty.

-or- uriString 中指定的方案格式不正确。The scheme specified in uriString is not correctly formed. 请参阅 CheckSchemeName(String)See CheckSchemeName(String).

-or- uriString 包含过多斜杠。uriString contains too many slashes.

-or- uriString 中指定的密码无效。The password specified in uriString is not valid.

-or- uriString 中指定的主机名无效。The host name specified in uriString is not valid.

-or- uriString 中指定的文件名无效。The file name specified in uriString is not valid.

-or- uriString 中指定的用户名无效。The user name specified in uriString is not valid.

-or- uriString 中指定的主机名或证书颁发机构名称不能以反斜杠结尾。The host or authority name specified in uriString cannot be terminated by backslashes.

-or- uriString 中指定的端口号无效或无法分析。The port number specified in uriString is not valid or cannot be parsed.

-or- uriString 的长度超过 65519 个字符。The length of uriString exceeds 65519 characters.

- 或 --or- uriString 中指定的方案的长度超过 1023 个字符。The length of the scheme specified in uriString exceeds 1023 characters.

-or- uriString 中存在无效的字符序列。There is an invalid character sequence in uriString.

-or- uriString 中指定的 MS-DOS 路径必须以 c:\\ 开头。The MS-DOS path specified in uriString must start with c:\\.

注解

相对和绝对 Uri 对其格式具有不同的限制。Relative and absolute URIs have different restrictions on their format. 例如,相对 URI 不需要方案或颁发机构。For example, a relative URI does not require a scheme or an authority. 在中uriKind指定的值必须与uriString传入的 URI 类型匹配。The value you specify in uriKind must match the type of URI passed in uriString. 但是,如果RelativeOrAbsolute指定了,则 URI 字符串可以是相对的,也可以是绝对的。However, if RelativeOrAbsolute is specified, the URI string can be relative or absolute.

Uri(Uri, String) Uri(Uri, String) Uri(Uri, String) Uri(Uri, String)

根据指定的基 URI 和相对 URI 字符串,初始化 Uri 类的新实例。Initializes a new instance of the Uri class based on the specified base URI and relative URI string.

public:
 Uri(Uri ^ baseUri, System::String ^ relativeUri);
public Uri (Uri baseUri, string relativeUri);
new Uri : Uri * string -> Uri
Public Sub New (baseUri As Uri, relativeUri As String)

参数

baseUri
Uri Uri Uri Uri

基 URI。The base URI.

relativeUri
String String String String

要添加到基 URI 的相对 URI。The relative URI to add to the base URI.

异常

baseUri 不是绝对的 Uri 实例。baseUri is not an absolute Uri instance.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 FormatExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

通过组合 baseUrirelativeUri 而构成的 URI 为空或仅包含空格。The URI formed by combining baseUri and relativeUri is empty or contains only spaces.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的方案无效。The scheme specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 包含过多的斜杠。The URI formed by combining baseUri and relativeUri contains too many slashes.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的密码无效。The password specified in the URI formed by combining baseUri and relativeUri is not valid.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的主机名无效。The host name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的文件名无效。The file name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的用户名无效。The user name specified in the URI formed by combining baseUri and relativeUri is not valid.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的主机名或证书颁发机构名称不能以反斜杠终止。The host or authority name specified in the URI formed by combining baseUri and relativeUri cannot be terminated by backslashes.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的端口号无效或无法分析。The port number specified in the URI formed by combining baseUri and relativeUri is not valid or cannot be parsed.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 的长度超过 65519 个字符。The length of the URI formed by combining baseUri and relativeUri exceeds 65519 characters.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的方案长度超过 1023 个字符。The length of the scheme specified in the URI formed by combining baseUri and relativeUri exceeds 1023 characters.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 中存在无效字符序列。There is an invalid character sequence in the URI formed by combining baseUri and relativeUri.

-or- uriString 中指定的 MS-DOS 路径必须以 c:\\ 开头。The MS-DOS path specified in uriString must start with c:\\.

示例

下面的示例Uri通过组合相对 uri http://www.contoso.comcatalog/shownew.htm形成绝对 URI http://www.contoso.com/catalog/shownew.htm来创建类的新实例。The following example creates a new instance of the Uri class by combining the relative URIs http://www.contoso.com and catalog/shownew.htm to form the absolute URI http://www.contoso.com/catalog/shownew.htm.

Uri^ baseUri = gcnew Uri(  "http://www.contoso.com" );
Uri^ myUri = gcnew Uri( baseUri, "catalog/shownew.htm" );
Console::WriteLine( myUri->ToString() );
Uri baseUri = new Uri("http://www.contoso.com");
 Uri myUri = new Uri(baseUri, "catalog/shownew.htm");

Console.WriteLine(myUri.ToString());
   
Dim baseUri As New Uri("http://www.contoso.com")
Dim myUri As New Uri(baseUri, "catalog/shownew.htm")

Console.WriteLine(myUri.ToString())

注解

此构造函数通过Uri baseUri组合和relativeUri来创建一个实例。This constructor creates a Uri instance by combining the baseUri and the relativeUri. 如果relativeUri是绝对 URI (包含方案、主机名和端口号(可选) Uri ,则仅relativeUri使用创建实例。If relativeUri is an absolute URI (containing a scheme, host name, and optionally a port number), the Uri instance is created using only relativeUri.

/api/ Uri baseUri如果具有相对部分(如/api),则相对部分必须以斜杠结尾(如),如果要在构造中保留的相关部分,则为。 baseUriIf the baseUri has relative parts (like /api), then the relative part must be terminated with a slash, (like /api/), if the relative part of baseUri is to be preserved in the constructed Uri.

此外,如果relativeUri以斜杠开头,则它将替换baseUriAdditionally, if the relativeUri begins with a slash, then it will replace any relative part of the baseUri

此构造函数不确保Uri引用辅助资源。This constructor does not ensure that the Uri refers to an accessible resource.

Uri(Uri, Uri) Uri(Uri, Uri) Uri(Uri, Uri) Uri(Uri, Uri)

根据指定的基 Uri 实例和相对 Uri 实例的组合,初始化 Uri 类的新实例。Initializes a new instance of the Uri class based on the combination of a specified base Uri instance and a relative Uri instance.

public:
 Uri(Uri ^ baseUri, Uri ^ relativeUri);
public Uri (Uri baseUri, Uri relativeUri);
new Uri : Uri * Uri -> Uri
Public Sub New (baseUri As Uri, relativeUri As Uri)

参数

baseUri
Uri Uri Uri Uri

作为新 Uri 实例的基的绝对 UriAn absolute Uri that is the base for the new Uri instance.

relativeUri
Uri Uri Uri Uri

Uri 组合的相对 baseUri 实例。A relative Uri instance that is combined with baseUri.

异常

baseUri 不是绝对的 Uri 实例。baseUri is not an absolute Uri instance.

baseUri 不是绝对的 Uri 实例。baseUri is not an absolute Uri instance.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 FormatExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

通过组合 baseUrirelativeUri 而构成的 URI 为空或仅包含空格。The URI formed by combining baseUri and relativeUri is empty or contains only spaces.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的方案无效。The scheme specified in the URI formed by combining baseUri and relativeUri is not valid.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 包含过多的斜杠。The URI formed by combining baseUri and relativeUri contains too many slashes.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的密码无效。The password specified in the URI formed by combining baseUri and relativeUri is not valid.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的主机名无效。The host name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的文件名无效。The file name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的用户名无效。The user name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的主机名或证书颁发机构名称不能以反斜杠终止。The host or authority name specified in the URI formed by combining baseUri and relativeUri cannot be terminated by backslashes.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的端口号无效或无法分析。The port number specified in the URI formed by combining baseUri and relativeUri is not valid or cannot be parsed.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 的长度超过 65519 个字符。The length of the URI formed by combining baseUri and relativeUri exceeds 65519 characters.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的方案长度超过 1023 个字符。The length of the scheme specified in the URI formed by combining baseUri and relativeUri exceeds 1023 characters.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中存在无效字符序列。There is an invalid character sequence in the URI formed by combining baseUri and relativeUri.

-or- uriString 中指定的 MS-DOS 路径必须以 c:\\ 开头。The MS-DOS path specified in uriString must start with c:\\.

示例

此示例将创建一个Uri绝对absoluteUri实例、 relativeUri和一个相对Uri实例。This example creates an absolute Uri instance, absoluteUri, and a relative Uri instance, relativeUri. 然后, Uri将从combinedUri这两个实例创建一个新的实例。A new Uri instance, combinedUri, is then created from these two instances.

// Create an absolute Uri from a string.
String^ addressString1 = "http://www.contoso.com/";
String^ addressString2 = "catalog/shownew.htm?date=today";
Uri^ absoluteUri = gcnew Uri(addressString1);

// Create a relative Uri from a string.  allowRelative = true to allow for 
// creating a relative Uri.
Uri^ relativeUri = gcnew Uri(addressString2);

// Check whether the new Uri is absolute or relative.
if (  !relativeUri->IsAbsoluteUri )
   Console::WriteLine( "{0} is a relative Uri.", relativeUri );

// Create a new Uri from an absolute Uri and a relative Uri.
Uri^ combinedUri = gcnew Uri( absoluteUri,relativeUri );
Console::WriteLine( combinedUri->AbsoluteUri );
// Create an absolute Uri from a string.
Uri absoluteUri = new Uri("http://www.contoso.com/");

// Create a relative Uri from a string.  allowRelative = true to allow for 
// creating a relative Uri.
Uri relativeUri = new Uri("/catalog/shownew.htm?date=today", UriKind.Relative);

// Check whether the new Uri is absolute or relative.
if (!relativeUri.IsAbsoluteUri)
    Console.WriteLine("{0} is a relative Uri.", relativeUri);

// Create a new Uri from an absolute Uri and a relative Uri.
Uri combinedUri = new Uri(absoluteUri, relativeUri);
Console.WriteLine(combinedUri.AbsoluteUri);
    ' Create an absolute Uri from a string.
    Dim absoluteUri As New Uri("http://www.contoso.com/")
    
    ' Create a relative Uri from a string.  allowRelative = true to allow for 
    ' creating a relative Uri.
    Dim relativeUri As New Uri("/catalog/shownew.htm?date=today")
    
    ' Check whether the new Uri is absolute or relative.
    If Not relativeUri.IsAbsoluteUri Then
        Console.WriteLine("{0} is a relative Uri.", relativeUri)
    End If 
    ' Create a new Uri from an absolute Uri and a relative Uri.
    Dim combinedUri As New Uri(absoluteUri, relativeUri)
    Console.WriteLine(combinedUri.AbsoluteUri)

End Sub 'SampleConstructor

注解

此构造函数通过将Uri绝对Uri实例baseUri与相对Uri实例relativeUri结合起来来创建一个新的实例。This constructor creates a new Uri instance by combining an absolute Uri instance, baseUri, with a relative Uri instance, relativeUri. 如果relativeUri是绝对Uri实例(包含方案、主机名和Uri端口号(可选),则仅relativeUri使用创建实例。If relativeUri is an absolute Uri instance (containing a scheme, host name, and optionally a port number), the Uri instance is created using only relativeUri.

/api/ Uri baseUri如果具有相对部分(如/api),则相对部分必须以斜杠结尾(如),如果要在构造中保留的相关部分,则为。 baseUriIf the baseUri has relative parts (like /api), then the relative part must be terminated with a slash, (like /api/), if the relative part of baseUri is to be preserved in the constructed Uri.

此外,如果relativeUri以斜杠开头,则它将替换baseUriAdditionally, if the relativeUri begins with a slash, then it will replace any relative part of the baseUri

此构造函数不确保Uri引用辅助资源。This constructor does not ensure that the Uri refers to an accessible resource.

Uri(Uri, String, Boolean) Uri(Uri, String, Boolean) Uri(Uri, String, Boolean) Uri(Uri, String, Boolean)

警告

此 API 现已过时。

根据指定的基 URI 和相对 URI,初始化 Uri 类的新实例,并对字符转义进行显式控制。Initializes a new instance of the Uri class based on the specified base and relative URIs, with explicit control of character escaping.

public:
 Uri(Uri ^ baseUri, System::String ^ relativeUri, bool dontEscape);
[System.Obsolete("The constructor has been deprecated. Please new Uri(Uri, string). The dontEscape parameter is deprecated and is always false. http://go.microsoft.com/fwlink/?linkid=14202")]
[System.Obsolete("The constructor has been deprecated. Please new Uri(Uri, string). The dontEscape parameter is deprecated and is always false. https://go.microsoft.com/fwlink/?linkid=14202")]
public Uri (Uri baseUri, string relativeUri, bool dontEscape);
new Uri : Uri * string * bool -> Uri
Public Sub New (baseUri As Uri, relativeUri As String, dontEscape As Boolean)

参数

baseUri
Uri Uri Uri Uri

基 URI。The base URI.

relativeUri
String String String String

要添加到基 URI 的相对 URI。The relative URI to add to the base URI.

dontEscape
Boolean Boolean Boolean Boolean

如果 uriString 已完全转义,则为 true;否则为 falsetrue if uriString is completely escaped; otherwise, false.

异常

baseUri 不是绝对的 Uri 实例。baseUri is not an absolute Uri instance.

通过组合 baseUrirelativeUri 而构成的 URI 为空或仅包含空格。The URI formed by combining baseUri and relativeUri is empty or contains only spaces.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的方案无效。The scheme specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 包含过多的斜杠。The URI formed by combining baseUri and relativeUri contains too many slashes.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的密码无效。The password specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的主机名无效。The host name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的文件名无效。The file name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的用户名无效。The user name specified in the URI formed by combining baseUri and relativeUri is not valid.

- 或 --or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的主机名或证书颁发机构名称不能以反斜杠终止。The host or authority name specified in the URI formed by combining baseUri and relativeUri cannot be terminated by backslashes.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的端口号无效或无法分析。The port number specified in the URI formed by combining baseUri and relativeUri is not valid or cannot be parsed.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 的长度超过 65519 个字符。The length of the URI formed by combining baseUri and relativeUri exceeds 65519 characters.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中指定的方案长度超过 1023 个字符。The length of the scheme specified in the URI formed by combining baseUri and relativeUri exceeds 1023 characters.

-or- 通过组合 baseUrirelativeUri 而构成的 URI 中存在无效字符序列。There is an invalid character sequence in the URI formed by combining baseUri and relativeUri.

- 或 --or- uriString 中指定的 MS-DOS 路径必须以 c:\\ 开头。The MS-DOS path specified in uriString must start with c:\\.

示例

下面的示例通过组合相对 uri Uri http://www.contoso.comHello%20World.htm形成一个绝对 URI 来创建类的新实例。The following example creates a new instance of the Uri class by combining the relative URIs http://www.contoso.com and Hello%20World.htm to form an absolute URI.

Uri^ baseUri = gcnew Uri( "http://www.contoso.com/" );
Uri^ myUri = gcnew Uri( baseUri,"Hello%20World.htm",false );
Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "Hello%20World.htm",false);

Dim baseUri As New Uri("http://www.contoso.com")
Dim myUri As New Uri(baseUri, "Hello%20World.htm", False)

注解

此构造函数通过Uri组合baseUrirelativeUri来创建一个实例。This constructor creates a Uri instance by combining baseUri and relativeUri. 如果传入relativeUri的 uri 是一个绝对 uri (包含方案、主机名和端口号(可选) Uri ,则仅relativeUri使用创建实例。If the URI passed in relativeUri is an absolute URI (containing a scheme, host name, and optionally a port number), the Uri instance is created using only relativeUri.

dontEscape参数控制是否将保留字符转换为转义序列。The dontEscape parameter controls whether reserved characters are translated into escape sequences. 仅当你确定 URI 中true的所有保留字符都已转义时,才应将此参数设置为。This parameter should be set to true only if you are certain that all reserved characters in the URI have been escaped. 对于未完全转义true的 URI,将值设置为会导致意外的行为。Setting the value to true for a URI that has not been completely escaped can cause unexpected behavior. 强烈建议您始终将此参数设置为falseIt is strongly recommended that you always set this parameter to false. 如果dontEscape设置为false,则构造函数将通过检查是否存在百分比(%)来转义任何保留字符后跟有效的转义序列。If dontEscape is set to false, the constructor escapes any reserved characters by checking that all occurrences of percent (%) are followed by a valid escape sequence. 如果百分号后面的字符序列无效,则百分比将替换为% 25。If the character sequence following a percent is not valid, the percent is replaced by %25.

此构造函数不确保Uri引用辅助资源。This constructor does not ensure that the Uri refers to an accessible resource.

调用方说明

出于安全方面的考虑,应用程序不应从不受信任的源中的 URI 字符串dontEscape调用此true构造函数,并将设置为。Because of security concerns, your application should not call this constructor with URI strings from untrusted sources and with dontEscape set to true. 或者,您可以在调用此构造函数之前通过调用IsWellFormedOriginalString()方法来检查 URI 字符串的有效性。Alternately, you can check a URI string for validity by calling the IsWellFormedOriginalString() method prior to calling this constructor.

适用于