Uri.IsWellFormedUriString(String, UriKind) 方法
定义
通过尝试用字符串构造一个 URI 来指示字符串是否为格式良好的,并确保字符串不需要进一步转义。Indicates whether the string is well-formed by attempting to construct a URI with the string and ensures that the string does not require further escaping.
public:
static bool IsWellFormedUriString(System::String ^ uriString, UriKind uriKind);
public static bool IsWellFormedUriString (string uriString, UriKind uriKind);
public static bool IsWellFormedUriString (string? uriString, UriKind uriKind);
static member IsWellFormedUriString : string * UriKind -> bool
Public Shared Function IsWellFormedUriString (uriString As String, uriKind As UriKind) As Boolean
参数
返回
如果该字符串格式正确,则为 true;否则为 false。true if the string was well-formed; otherwise, false.
注解
在版本4.5 之前的 .NET 版本中,默认情况下,根据 RFC 2396 和 RFC 2732,将字符串视为格式正确。In versions of .NET before version 4.5, by default the string is considered well-formed in accordance with RFC 2396 and RFC 2732. 如果国际资源标识符 (IRIs) 或国际化域名 (IDN) 分析已启用,则根据 RFC 3986 和 RFC 3987,字符串被视为格式正确。If International Resource Identifiers (IRIs) or Internationalized Domain Name (IDN) parsing are enabled, the string is considered well-formed in accordance with RFC 3986 and RFC 3987.
从 .NET 4.5 开始,无论是否启用了 IRI 或 IDN,字符串始终根据 RFC 3986 和 RFC 3987 被视为格式正确。Beginning in .NET 4.5, strings are always considered well-formed in accordance with RFC 3986 and RFC 3987, whether or not IRI or IDN are enabled. 但请注意,这仅适用于面向 .NET 4.5 或更高版本的应用程序。Note, however, that this is only true for apps that target .NET 4.5 or later. 面向 .NET 4.0 的应用将调用兼容性代码,并体验旧 (4.5 之前) 的行为。Apps that target .NET 4.0 invoke compatibility code, and experience the old (pre-4.5) behavior.
如果出现下列任一情况,字符串将被视为格式不正确,从而导致方法返回 falseThe string is considered poorly formed, causing the method to return false, if any of the following conditions occur
| 错误Error | 示例Example |
|---|---|
| 字符串未正确转义。The string is not correctly escaped. | http://www.contoso.com/path???/file name |
| 字符串是 Uri 表示隐式文件的绝对 Uri 。The string is an absolute Uri that represents an implicit file Uri. | c:\\directory\filename |
| 字符串是一个绝对 URI,它在路径之前缺少一个斜杠。The string is an absolute URI that is missing a slash before the path. | file://c:/directory/filename |
| 该字符串包含非转义反斜杠,即使它们将被视为正斜杠The string contains unescaped backslashes even if they will be treated as forward slashes | http:\\\host/path/file |
| 该字符串表示层次结构的绝对路径 Uri ,并且不包含 "://"The string represents a hierarchical absolute Uri and does not contain "://" | www.contoso.com/path/file |
| 的分析器 Scheme 指示原始字符串格式不正确。The parser for the Scheme indicates that the original string was not well-formed. | 该示例取决于 URI 的方案。The example depends on the scheme of the URI. |
| 从 .NET 4.5 开始,其第一段中 ) 带有冒号 ( ":" 的相对 Uri 不被视为格式正确。Beginning with .NET 4.5, relative URIs with a colon (':') in their first segment are not considered well-formed. | 2013.05.29_14:33:41 |
有关 IRI 支持的详细信息,请参阅类的 "备注" 部分 Uri 。For more information on IRI support, see the Remarks section for the Uri class.