C#String.Format - 输入字符串无效

Sam*_*Sam 8 c# string.format

我有一个像这样的MVC3 HtmlHelper扩展:

public static MvcHtmlString ShowInfoBar(this HtmlHelper helper, string message, InfoMessageType messageType)
    {
        return MvcHtmlString.Create(String.Format(@"<script type=""text/javascript"">$(document).ready(function () { gtg.core.showInfoBar('{0}', '{1}'}; );</script>", message, messageType.ToString().ToLower()));
    }
Run Code Online (Sandbox Code Playgroud)

价值message"The product "Product Name" was saved successfully."

价值messageTypeinfo.

它一直在说 Input string was not in the correct format.

我卡住了?

Mar*_*ell 20

在每个不是令牌的支撑上你必须加倍 - 所以

function() {{
Run Code Online (Sandbox Code Playgroud)

等等

另外 - 在这里考虑XSS - 是否message正确转义以插入JavaScript?


Lou*_*cci 10

在格式字符串中转义您的波浪括号{{}}

String.Format(@"<script type=""text/javascript"">$(document).ready(function () {{ gtg.core.showInfoBar('{0}', '{1}'); }});</script>", message, messageType.ToString().ToLower())
Run Code Online (Sandbox Code Playgroud)


Mrc*_*ief 5

你需要逃避花括号:

{{}}

String.Format(@"<script type=""text/javascript"">$(document).ready(function () {{ gtg.core.showInfoBar('{0}', '{1}'}}; );</script>", 
              message, messageType.ToString().ToLower())
Run Code Online (Sandbox Code Playgroud)