gpt4 book ai didi

c# - 为什么会抛出字符串格式异常

转载 作者:行者123 更新时间:2023-11-30 18:58:53 27 4
gpt4 key购买 nike

只见树木不见森林。

为什么会抛出字符串格式异常?

 private const string GoogleAnalyticsFormat = @"<script type=""text/javascript"">
var _gaq = _gaq || [];

_gaq.push(['_setAccount', '{0}']);
_gaq.push(['_trackPageview']);

(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>";

public static IHtmlString RenderGoogleAnalytics<T>(this HtmlHelpers<T> html, string trackingCode )
{
return html.Raw(string.Format(GoogleAnalyticsFormat, trackingCode));
}

最佳答案

看看你的格式字符串的这一点:

function () { ... }

那些大括号被解释为占位符的开始/结束。你需要将它们加倍:

function () {{ ... }}

因此您的完整声明将是:

private const string GoogleAnalyticsFormat = @"<script type=""text/javascript"">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '{0}']);
_gaq.push(['_trackPageview']);
(function () {{
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
}})();
</script>";

关于c# - 为什么会抛出字符串格式异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18318914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com