将 C# 字符串编码为 Javascript 字符串的注意事项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9413421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:35:48  来源:igfitidea点击:

Caveats Encoding a C# string to a Javascript string

c#javascriptserializationencoding

提问by Machado

I'm trying to write a custom Javascript MVC3 Helper class foe my project, and one of the methods is supposed to escape C# strings to Javascript strings.

我正在尝试为我的项目编写一个自定义的 Javascript MVC3 Helper 类,其中一种方法应该将 C# 字符串转义为 Javascript 字符串。

I know C# strings are UTF-16 encoded, and Javascript strings also seem to be UTF-16. No problem here.

我知道 C# 字符串是UTF-16 编码的,而 Javascript 字符串似乎也是 UTF-16。这里没问题。

I know some characters like backslash, single quotes or double quotes must be backslash-escaped on Javascript so:

我知道一些字符,如反斜杠、单引号或双引号必须在 Javascript 上进行反斜杠转义,因此:

\ becomes \
' becomes \'
" becomes \"

Is there any other caveat I must be aware of before writing my conversion method ?

在编写我的转换方法之前,还有什么我必须注意的警告吗?

EDIT: Great answers so far, I'm adding some references from the answers in the question to help others in the future.

编辑:到目前为止,很好的答案,我正在从问题的答案中添加一些参考资料,以帮助将来的其他人。

Alex K. suggested using System.Web.HttpUtility.JavaScriptStringEncode, which I marked as the right answer for me, because I'm using .Net 4. But this function is not available to previous .Net versions, so I'm adding some other resources here:

Alex K. 建议使用System.Web.HttpUtility.JavaScriptStringEncode,我将其标记为正确答案,因为我使用的是 .Net 4。但此功能不适用于以前的 .Net 版本,因此我在这里添加了一些其他资源:

CR  becomes \r   // Javascript string cannot be broke into more than 1 line
LF  becomes \n   // Javascript string cannot be broke into more than 1 line
TAB becomes \t

Control characters must be Hex-Escaped

JP Richardsongave an interesting link informing that Javascript uses UCS-2, which is a subset of UTF-16, but how to encode this correctly is an entirely new question.

JP Richardson给出了一个有趣的链接,告知 Javascript 使用 UCS-2,它是 UTF-16 的一个子集,但如何正确编码它是一个全新的问题。

LukeH on the comments below reminded the CR, LF and TAB chars, and that reminded me of the control chars (BEEP, NULL, ACK, etc...).

LukeH 在下面的评论中提醒了 CR、LF 和 TAB 字符,这让我想起了控制字符(BEEP、NULL、ACK 等...)。

回答by Alex K.

(.net 4) You can;

(.net 4) 你可以;

System.Web.HttpUtility.JavaScriptStringEncode(@"aa\bb ""cc"" dd\tee", true);
== 
"aa\bb \"cc\" dd\tee"

回答by JP Richardson

It's my understanding that you do have to be careful, as JavaScript is not UTF-16, rather, it's UCS-2 which I believe is a subset of UTF-16. What this means for you, is that any character that is represented than a higher code point of 2 bytes (0xFFFF) could give you problems in JavaScript.

我的理解是您必须小心,因为 JavaScript 不是 UTF-16,而是 UCS-2,我认为它是 UTF-16 的子集。这对您来说意味着,任何比 2 字节 (0xFFFF) 更高的代码点表示的字符都可能会给您带来 JavaScript 问题。

In summary, under the covers, the engine may use UTF-16, but it only exposes UCS-2 like methods.

总之,在幕后,引擎可能使用 UTF-16,但它只公开类似 UCS-2 的方法。

Great article on the issue: http://mathiasbynens.be/notes/javascript-encoding

关于这个问题的好文章:http: //mathiasbynens.be/notes/javascript-encoding

回答by Chris Gessler

Just use Microsoft.JScript.GlobalObject.escape

只需使用 Microsoft.JScript.GlobalObject.escape

Found it here: http://forums.asp.net/p/1308104/4468088.aspx/1?Re+C+equivalent+of+JavaScript+escape+

在这里找到:http: //forums.asp.net/p/1308104/4468088.aspx/1?Re+C+equivalent+of+JavaScript+escape+

回答by AndyBre

Instead of using JavaScriptStringEncode()method, you can encode server side using:

JavaScriptStringEncode()您可以使用以下方法对服务器端进行编码,而不是使用方法:

HttpUtility.UrlEncode()

When you need to read the encoded string client side, you have to call unescape()javascript function before using the string.

当您需要读取编码后的字符串客户端时,您必须unescape()在使用字符串之前调用javascript 函数。