在JavaScript内部的内联c#脚本中转义双引号 [英] Escaping a double-quote in inline c# script within javascript

查看:129
本文介绍了在JavaScript内部的内联c#脚本中转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在JavaScript中的内联c#中避免双引号。代码如下:

I need to escape a double quote in inline c# within javascript. Code is below:

if ("<%= TempData["Message"]%>" == "") {
    // code
};

通常我只会使用单引号:

Normally, I would just use single quotes like so:

if ('<%= TempData["Message"]%>' == "") {
    // code
};

但是, TempData [Message] 在其中包含单引号(当它包含由ASP.NET MVC中的Html.ActionLink()帮助器生成的链接时)。所以尽管我可以将 TempData [Message] 中的所有ActionLink帮助者更改为标签,但这是一个有趣的问题,如果有人有任何答案,那么他们会很乐意。 p>

However, TempData["Message"] has single quotes within it (when it contains a link generated by the Html.ActionLink() helper in ASP.NET MVC). So while I could change all the ActionLink helpers inside TempData["Message"] to tags, it's an interesting problem and would been keen to hear if anyone has an answer.

推荐答案

调用 HttpUtility.JavaScriptStringEncode

此方法是ASP.Net 4.0的新功能;对于早期版本,请使用 WPL

这篇关于在JavaScript内部的内联c#脚本中转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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