我将ServiceStack (v3.9.44.0) 用作 Windows 服务(针对 .Net4.5),并使用Razor让 ServiceStack 生成和提供 HTML。
一切运行良好,但有一个奇怪的问题:视图.cshtml
中的硬编码 unicode 字符串似乎没有正确呈现。
这就是我的意思:
项目视图的结构是规范的:
存在_Layout.cshtml
:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<p>Accents in \Views\_Layout.cshtml: à é ë ô</p>
@RenderBody()
</body>
</html>
和Articles.cshtml
:
@inherits ViewPage<List<PrepaService.Article>>
<p>Accents in \Views\Articles.cshtml: à é ë ô</p>
Table with code-injected list of items:
<table>
@foreach(var a in Model) {
<tr>
<td>@a.Description</td>
</tr>
}
</table>
这两个文件都正确保存为无 BOM 的 UTF8 文件。
我还尝试添加以下内容,但它不会改变任何内容:
SetConfig(new EndpointHostConfig {
AppendUtf8CharsetOnContentTypes = new HashSet<string> { ContentType.Html }
});
所以,回顾一下:
我对所有文件和显式
charset=utf-8
内容类型定义使用无 BOM 的 UTF8 编码。这适用于
_Layout.cshtml
在视图模板中呈现的任何内容,以及由代码注入的任何内容但它不适用于 View templates 中的硬编码字符串。