2013-08-19 2 views
2

나는 나무에 대한 나무를 볼 수 없습니다.왜이 문자열 형식 예외 던지고

왜 이것이 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)); 
} 
+0

에 괄호를 {을 변경해야 할 당신이'{ '와'}'당신의 형식 끈. 당신은 그것들을 벗어날 필요가 있습니다. –

답변

10

봐 :

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>";