为什么Uri对不同的方案表现不同? [英] Why does Uri behave differently for different schemes?
问题描述
在用Uri类回答另一个问题时,我发现了一些对我来说很奇怪的东西:
While poking around with the Uri class answering another question, I found something that seems strange to me:
考虑这两个Uris:
var u1 = new Uri("http://a.b:33/abc%2fdef/c?d=f");
var u2 = new Uri("foobar://a.b:33/abc%2fdef/c?d=f");
它们的区别仅在于它们的方案。所提供的标识符的所有其他元素是相同的。
They differ only by their scheme. All other elements of the supplied identifiers are the same.
所以,为什么,当我转储 Segments
属性这些Uri实例,我看到 u1
的以下输出:
So, why, when I dump the Segments
property of these Uri instances, do I see the following output for u1
:
/
abc/
def/
c
...但不同的输出为 u2
?
...but a different output for u2
?
/
abc%2fdef/
c
为什么不同方案的解析行为不同?
Why is the the parsing behaviour different for different schemes?
推荐答案
Uri类对不同的URI方案使用不同的解析器。例如,对于 http 和 https URI,它使用 HttpStyleUriParser ,而对于 ftp URI,它使用 FtpStyleUriParser ,等等。具有未知方案的URI由 GenericUriParser 解析。您可以使用 UriParser.Register方法注册新方案。
The Uri Class uses different parsers for different URI schemes. For example, for http and https URIs, it uses a HttpStyleUriParser, while for ftp URIs it uses an FtpStyleUriParser, and so on. URIs with unknown schemes are parsed by a GenericUriParser. You can register new schemes using the UriParser.Register Method.
UriParser.Register(new HttpStyleParser(), "foobar", 33);
这篇关于为什么Uri对不同的方案表现不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!