c# - Why is this throwing a string format exception -
i can't see wood trees.
why throwing string format exception?
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)); }
look @ bit of format string:
function () { ... }
those braces being interpreted start/end of placeholders. need double them:
function () {{ ... }}
so complete declaration be:
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>";
Comments
Post a Comment