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

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

问题描述

一个奇怪的问题,但让我们看看它得到什么样的回应......

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

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

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

Uri.EscapeUriString("[")

我明白了:

[

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

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

%5B

为了进一步复杂化,根据这篇文章 Uri.EscapeUriString("[") 确实应该返回 %5B.这篇文章写于 27/06/2012.

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

我认为 LINQPad 引用的 DLL 可能比 VS 使用的旧,但这意味着 EscapeUriString 相对最近发生了变化,我找不到任何记录.有没有人对可能导致这种行为的原因有任何想法?

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?

推荐答案

这在 .Net 4 和 .Net 4.5 之间发生了变化,您可以通过将框架版本重定向到 .Net 4 并运行测试程序来验证这一点.

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 ->输出%5B".net 4.5(或更高版本)->输出["

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

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

Uri.EscapeDataString 、Uri.EscapeUriString 和 Uri.UnescapeDataString 部分下,其中指出(使用 .Net 4.5):

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

保留和非保留字符列表现在支持 RFC 3986.

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

具体变化:

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.

特别是,EscapeUriString 不会对保留字符进行转义,这很重要.

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

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