response.write 编码错误
我的网络应用程序中有一个字符串 字符串是这样的:
`1234567890-=[]\ ;',./\~!@#$%^&*()_+{}|:"<>?|
编码后(通过使用 Server.Encode() )它显示以下内容:
`1234567890-=[]\\ ;',./\\~!@#$%^&*()_+{}|:"<>?|
这是正确的
但是,当我使用 Response.Write(theSecondExample) 时,结果是这样的
`1234567890-=[]\ ;',./\~!@#$%^&*()_+{}|:"<>?|
:缺少反斜杠!
怎么输出结果不是我预期的呢?我该如何预防?
I have a string in a web application
The string is like this :
`1234567890-=[]\ ;',./\~!@#$%^&*()_+{}|:"<>?|
after encoding (by using Server.Encode() ) it show the following :
`1234567890-=[]\\ ;',./\\~!@#$%^&*()_+{}|:"<>?|
which is correct
However , when I use Response.Write(theSecondExample)
the result is like this :
`1234567890-=[]\ ;',./\~!@#$%^&*()_+{}|:"<>?|
The backslashes are missing!
How can it be that the output is not what I expected? How can I prevent it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有错误 - 您正在调试器中验证字符串,它会自动转义字符串 - 例如
"Hello \ Goodbye"
将在调试器中显示为"Hello \\ Goodbye".
也就是说,调试器的行为有所不同,具体取决于您查看字符串的方式(当然还有它是否是 C#/VB):
更新
好的,我已经更进一步并启动了 VS2010,请创建一个测试项目并按照它进行。
(显然,我最初使用了逐字字符串,以避免必须转义除双引号之外的任何内容)。
测试的控制台输出为:
同样,如果您在测试结束时断点并开始使用可视化工具进行处理:
Hover (
a
):即它是在工具提示中转义的 C# 并用引号括起来。
Hover (
htmlEncoded
):.. 再次,它是 html 编码和 C# 用引号转义
Text (
htmlEncoded
):.. 没有 c# 转义
Html (
htmlEncoded
):当然是用 Times New Roman 脚本:)
我相信这会让我们回到原始字符串 - 这也表明您所描述的场景不能情况就是如此 - 除非您将转义字符串视为“正确”,但实际上并非如此。 Html 不需要对
\
进行转义。There is no error - you're verifying the string in the debugger, which automatically escapes strings - e.g.
"Hello \ Goodbye"
will show in the debugger as"Hello \\ Goodbye"
.That said, the debugger behaves differently depending on how you view a string (and also whether it's C#/VB of course):
Update
Okay, so I've gone a bit further and fired up VS2010, please create a test project and follow it through.
(obviously I've used a verbatim string initially to avoid having to escape anything except the double quote).
Console output of the test is:
Equally if you breakpoint the end of the test and start mucking about with the visualisers:
Hover (
a
):i.e. it's C# escaped in the tooltip and surrounded by quotes.
Hover (
htmlEncoded
):.. again, it's html encoded and C# escaped with quotes
Text (
htmlEncoded
):.. No c# escaping
Html (
htmlEncoded
):In Times New Roman script of course :)
Which I believe takes us back to the original string - which also shows that the scenario you're describing can't be the case - unless you have read an escaped string as being "correct", when in fact it's not. Html doesn't require
\
to be escaped.