在下面的测试中,为什么(只有)最后一个失败了?
[Fact]
public void IsWellFormedUriString_AbsolutNonHashTagUri_ReturnsTrue()
{
Assert.True(Uri.IsWellFormedUriString("http://www.RegularSite.org/Home", UriKind.Absolute));
}
[Fact]
public void IsWellFormedUriString_RelativeNonHashTagUri_ReturnsTrue()
{
Assert.True(Uri.IsWellFormedUriString("Home", UriKind.Relative));
}
[Fact]
public void IsWellFormedUriString_AbsolutHashTagUri_ReturnsTrue()
{
Assert.True(Uri.IsWellFormedUriString("http://www.w3.org/#!Home", UriKind.Absolute));
}
[Fact]
public void IsWellFormedUriString_RelativeHashTagUri_ReturnsTrue()
{
// Fails!
Assert.True(Uri.IsWellFormedUriString("#!Home", UriKind.Relative));
}
如果 Uri
识别 Hashbangs在 IsWellFormedUriString
的绝对版本中,为什么不在相对版本中?我错过了什么?
备注:This没有帮助。
最佳答案
之所以没有像您预期的那样工作,是因为 hashbang 不是 URI Scheme 的一部分。 .该方法需要 URI 格式的分层部分,而散列标记(以及随后的散列标记)不是确定相对和绝对路径的分层部分的成员。
< >是必填部分
[ ] 是可选部分
<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]
作为绝对 URI 的示例;如果我没记错的话,查询将被忽略,其中包括片段和散列标记
http://domain.com/path/to/something/?query=1#fragment
这里还为您提供了更多信息。这全部来自描述 Uri.IsWellFormedUriString()
的 MSDN方法
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.
备注:
By default, the string is considered well-formed in accordance with RFC 2396 and RFC 2732. If International Resource Identifiers (IRIs) or Internationalized Domain Name (IDN) parsing is enabled, the string is considered well-formed in accordance with RFC 3986 and RFC 3987.
The string is considered poorly formed, causing the method to return false, if any of the following conditions occur
The following are some failure examples:
http://www.contoso.com/path???/file name
The string is not correctly escaped.c:\directory\filename
The string is an absolute Uri that represents an implicit file Uri.file://c:/directory/filename
The string is an absolute URI that is missing a slash before the path.http:\host/path/file
The string contains unescaped backslashes even if they will be treated as forward slasheswww.contoso.com/path/file
The string represents a hierarchical absolute Uri and does not contain "://"
关于c# - Uri.IsWellFormedUriString 用于相对 Hashbang url 兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11948523/