问题描述
为什么EscapeDataString表现.NET 4和4.5之间的不同?输出是
Why does EscapeDataString behave differently between .NET 4 and 4.5? The outputs are
-
Uri.EscapeDataString(-_〜*'()!)=> _。!〜*'()
Uri.EscapeDataString(-_〜*'()!)=> -_%〜21%2A%27%28%29
默认情况下,EscapeDataString方法将所有字符,除了 为RFC 2396毫无保留的字符的十六进制 再presentation。如果国际资源标识符(IRIS)或 国际化域名(IDN)解析可用时, EscapeDataString方法将所有字符,除了RFC 3986 毫无保留的人物,他们的十六进制重新presentation。所有 统一code字符转义之前转换为UTF-8格式。
有关参考,毫无保留的字符被定义在 RFC如下2396 :
For reference, unreserved characters are defined as follows in RFC 2396:
unreserved = alphanum | mark
mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
(" | ")"
而在 RFC 3986 :
ALPHA / DIGIT / "-" / "." / "_" / "~"
源$ C $ C
它看起来像EscapeDataString的每个字符是否被转义大致确定这样
The source code
It looks like whether each character of EscapeDataString is escaped is determined roughly like this
is unicode above \x7F
? PERCENT ENCODE
: is a percent symbol
? is an escape char
? LEAVE ALONE
: PERCENT ENCODE
: is a forced character
? PERCENT ENCODE
: is an unreserved character
? PERCENT ENCODE
这是在这最后的检查是一个毫无保留的人物,其中RFC2396和RFC3986之间作出选择。该方法的来源$ C $ C逐字是
It's at that final check "is an unreserved character" where the choice between RFC2396 and RFC3986 is made. The source code of the method verbatim is
internal static unsafe bool IsUnreserved(char c)
{
if (Uri.IsAsciiLetterOrDigit(c))
{
return true;
}
if (UriParser.ShouldUseLegacyV2Quirks)
{
return (RFC2396UnreservedMarks.IndexOf(c) >= 0);
}
return (RFC3986UnreservedMarks.IndexOf(c) >= 0);
}
这code指的是
And that code refers to
private static readonly UriQuirksVersion s_QuirksVersion =
(BinaryCompatibility.TargetsAtLeast_Desktop_V4_5
// || BinaryCompatibility.TargetsAtLeast_Silverlight_V6
// || BinaryCompatibility.TargetsAtLeast_Phone_V8_0
) ? UriQuirksVersion.V3 : UriQuirksVersion.V2;
internal static bool ShouldUseLegacyV2Quirks {
get {
return s_QuirksVersion <= UriQuirksVersion.V2;
}
}
困惑
这似乎是矛盾的,该文件说EscapeDataString的输出取决于IRI / IDN分析是否被激活,而源$ C $ C说,输出由 TargetsAtLeast_Desktop_V4_5 $的值决定C $ C>。可能有人清楚这件事?
Confusion
It seems contradictory that the documentation says the output of EscapeDataString depends on whether IRI/IDN parsing is enabled, whereas the source code says the output is determined by the value of TargetsAtLeast_Desktop_V4_5
. Could someone clear this up?
推荐答案
一个很大的变化已经完成4.5在系统功能方面以及它如何比较到4.0。U可以看看这个线程
A lot of changes has been done in 4.5 comparing to 4.0 in terms of system functions and how it behaves.U can have a look at this thread
Why相比于我的机器并Uri.EscapeDataString回到我的CI服务器上不同的结果?
或
U可以直接去下面的链接
U can directly go to the following link
http://msdn.microsoft.com /en-us/library/hh367887(v=vs.110).aspx
这一切已经与来自世界各地的用户输入。
All this has been with the input from the users around the world.
这篇关于Uri.EscapeDataString怪事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!