In the following tests, why does (only) the last one fail?
[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));
}
If Uri recognizes Hashbangs in the Absolute version of IsWellFormedUriString, why not in the Relative version? What am I missing?
Note: This doesn’t help.
The reason this isn’t working as you’d expect is because a hashbang isn’t part of the URI Scheme. The method is expecting the hierarchical part of the URI format and a hash mark (and subsequently a hashbang) is not a member of the hierarchical part from which a relative and absolute path is determined.
< > is a required part
[ ] is an optional part
<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]as an example of an absolute URI; the query, if I’m not mistaken is ignored, which includes the fragment and hash mark
http://domain.com/path/to/something/?query=1#fragmentHere is some more information for you as well. This is all from the MSDN describing the
Uri.IsWellFormedUriString()methodRemarks: