Uri.EscapeUriString与方括号 [英] Uri.EscapeUriString with square braces

查看:336
本文介绍了Uri.EscapeUriString与方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我编写一个控制台应用程序(VS 2013,.NET 4.5.1 )并执行这一行代码:



Uri.EscapeUriString([]



我得到这个:



[



但是,如果我在我机器上的LINQPad中执行相同的事情(在技术上执行 Uri.EscapeUriString([)。Dump())得到这个:



%5B



复杂的事情,根据这篇文章 Uri.EscapeUriString([] 应该确实返回%5B 。该帖子是写在27/06/2012。



我是认为也许LINQPad引用了比VS使用的更旧的DLL,但这意味着 EscapeUriString 已经相对更新了,我找不到任何记录。有没有人有什么想法可能导致这种行为?

解决方案

这个在.Net 4和.Net 4.5之间有所变化,您可以通过将框架版本重新定位到.Net 4并运行测试程序来验证。



.Net 4 - >输出%5B
.net 4.5(或更新) - >输出[



这里提到: .NET Framework 4.5中的应用程序兼容性



对于 Uri.EscapeDataString,Uri.EscapeUriString和Uri.UnescapeDataString ,其中指出(与.Net 4.5):


保留和未保留字符的列表现在支持 RFC 3986



具体更改:

 未保留的转义字符未转义。 
EscapeDataString转义基于RFC 3986的保留字符。
EscapeUriString不会转义保留字符。
如果遇到无效的转义序列,UnescapeDataString不会引发异常。


特别是,EscapeUriString不会转义


Something of a strange question but let's see what kind of response it gets...

If I code a console app (VS 2013, .NET 4.5.1) and execute this line of code:

Uri.EscapeUriString("[")

I get this:

[

However if I execute the same thing (well, technically Uri.EscapeUriString("[").Dump()) in LINQPad on my machine I get this:

%5B

To further complicate things, according to this post Uri.EscapeUriString("[") should indeed return %5B.The post was written on 27/06/2012.

I'm thinking that perhaps LINQPad is referencing an older DLL than that used by VS, but that would imply that EscapeUriString has changed relatively recently, which I cannot find any record of. Does anyone have any ideas as to what could be causing this behaviour?

解决方案

This changed between .Net 4 and .Net 4.5, which you can verify by retargeting the framework version to .Net 4 and running the test program.

.Net 4 -> outputs "%5B" .net 4.5 (or later) -> outputs "["

This is mentioned here: Application Compatibility in the .NET Framework 4.5

under the section for Uri.EscapeDataString , Uri.EscapeUriString, and Uri.UnescapeDataString, which states that (with .Net 4.5):

The list of reserved and unreserved characters now supports RFC 3986.

Specific changes:

Unreserved escaped characters are un-escaped.
EscapeDataString escapes reserved characters based on RFC 3986.
EscapeUriString does not escape reserved characters.
UnescapeDataString does not throw an exception if it encounters an invalid escape sequence.

In particular, it is the EscapeUriString does not escape reserved characters which is significant.

这篇关于Uri.EscapeUriString与方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆