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

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

问题描述

我要逃避联C#的JavaScript中的一个双引号。代码如下:

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 [消息] 里面出现单引号(当它包含在ASP.NET MVC的Html.ActionLink()辅助生成的链接)。因此,虽然我可以改变内部所有ActionLink的佣工的TempData [消息] 来标记,这是一个有趣的问题,并会一直热衷于听到的话,任何人有一个答案。

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天全站免登陆