String.Format() - 输入字符串的格式不正确

Con*_*eak 0 c# asp.net-mvc razor

String.Format()方法上得到这个错误并且不明白为什么,一切对我来说都很好看?

string fontface = "";

foreach(...)
{
  fontface = String.Format(@"{3}
    @font-face {
        font-family: '{0}';
        src: url('{0}.eot');
        src: url('{0}.eot') format('embedded-opentype'),
                url('{0}.woff2') format('woff2'),
                url('{0}.woff') format('woff'),
                url('{0}.ttf') format('truetype'),
                url('{0}.svg#{0}') format('svg');
        font-weight:{1};
        font-style:{2};
    }"
  , family, weight, style, fontface);
}
Run Code Online (Sandbox Code Playgroud)

Rah*_*thi 8

你需要"{", "}"通过将它们加倍来string.Format()考虑每个'{''}'作为占位符的一部分(如'{0}')

像这样的东西:

fontface = String.Format(@"{3}
    @font-face {{
        font-family: '{0}';
        src: url('{0}.eot');
        src: url('{0}.eot') format('embedded-opentype'),
                url('{0}.woff2') format('woff2'),
                url('{0}.woff') format('woff'),
                url('{0}.ttf') format('truetype'),
                url('{0}.svg#{0}') format('svg');
        font-weight:{1};
        font-style:{2};
    }}"
  , family, weight, style, fontface);
Run Code Online (Sandbox Code Playgroud)