C# String.Format - Invalid input string

前端 未结 5 456
轮回少年
轮回少年 2021-01-12 05:11

I have a MVC3 HtmlHelper extension like this:

public static MvcHtmlString ShowInfoBar(this HtmlHelper helper, string message, InfoMessageType messageType)
           


        
相关标签:
5条回答
  • 2021-01-12 05:52

    Escape your squiggly brackets {{ }} in the format string

    String.Format(@"<script type=""text/javascript"">$(document).ready(function () {{ gtg.core.showInfoBar('{0}', '{1}'); }});</script>", message, messageType.ToString().ToLower())
    
    0 讨论(0)
  • 2021-01-12 05:59

    I have tried as below, and its working.

    string.Format(@"ExecuteOrDelayUntilScriptLoaded(function () {{ Your function. 
    
    0 讨论(0)
  • 2021-01-12 06:05

    On every brace that isn't a token you must double - so

    function() {{
    

    Etc

    Also - consider XSS here - is message escaped correctly for inserting into JavaScript?

    0 讨论(0)
  • 2021-01-12 06:06

    You need to escape the curly braces:

    {{ and }}

    String.Format(@"<script type=""text/javascript"">$(document).ready(function () {{ gtg.core.showInfoBar('{0}', '{1}'}}; );</script>", 
                  message, messageType.ToString().ToLower())
    
    0 讨论(0)
  • 2021-01-12 06:09

    In my case I used the bracket for JsonP formatting. JsonP requires a '{' too. By escaping the { like this: '{{', my problem was solved.

    0 讨论(0)
提交回复
热议问题