C#String.Format-输入字符串无效
我有一个MVC3 HtmlHelper扩展,如下所示:C#String.Format-输入字符串无效,c#,string.format,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).read
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()));
}
public static MvcHtmlString ShowInfoBar(此HtmlHelper帮助程序,字符串消息,InfoMessageType messageType)
{
返回MvcHtmlString.Create(String.Format(@“$(document).ready(函数(){gtg.core.showInfoBar({0},{1};);”,message,messageType.ToString().ToLower());
}
消息
的值为“已成功保存产品”product Name“。
messageType
的值为info
它一直在说输入字符串的格式不正确。
我被卡住了???在格式字符串中摆脱你的扭曲括号{{}}
String.Format(@"<script type=""text/javascript"">$(document).ready(function () {{ gtg.core.showInfoBar('{0}', '{1}'); }});</script>", message, messageType.ToString().ToLower())
String.Format(@“$(document).ready(函数(){{gtg.core.showInfoBar({0}',{1}');}}});”,消息,消息类型.ToString().ToLower()
您需要避开大括号:
{{
和}
String.Format(@"<script type=""text/javascript"">$(document).ready(function () {{ gtg.core.showInfoBar('{0}', '{1}'}}; );</script>",
message, messageType.ToString().ToLower())
String.Format(@“$(document).ready(函数(){{gtg.core.showInfoBar({0},{1}}};);”,
message,messageType.ToString().ToLower())
在每个不是标记的大括号上,必须加倍-so
function() {{
等
也考虑XSS在这里——<代码>消息>代码>插入到JavaScript中是正确的?
在我的例子中,我使用了JSONP格式的括号。jSONP也需要一个{ }。通过这样的{ { } },我的问题得到了解决。 我尝试如下,并且它的工作。string.Format(@"ExecuteOrDelayUntilScriptLoaded(function () {{ Your function.
如果您只输入字符串而不使用string.Format()方法,是否会出错?您能给我指个地方进一步了解一下吗?谢谢!哪个,XSS?任何地方。Wikipedia将是一个开始。您只需要在中输入一条带有引号的“消息”来测试它(尽管完全转义不仅仅是引号)很好地提到了JavaScript是否正确转义了方法参数。@Sam right;所以现在它被破坏了。现在想象一下,我小心地创建了一个产品名称,如
”;StealySourcookiesandPostThemmome();//
-我已经侵入了您的登录-因此是XSS。最简单的方法是将数据写入某个隐藏元素并读取。另一种方法是使用类似JavascriptSerializer的东西来编写JSON(然后将其转义)。一个简单的JSON转义也是受欢迎的。@Sam这当然取决于上下文-你可能会说不能正确处理O'Neills的饼干就足够了。但也不要低估组织内部所犯下的罪恶(特别是在糟糕的薪酬审查等之后不久)-不仅仅是未洗的公众能够造成伤害。