【发布时间】:2013-05-09 02:39:14
【问题描述】:
我使用 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 }
});
所以,回顾一下:
我正在对所有文件使用无 BOM 的 UTF8 编码和明确的
charset=utf-8内容类型定义。这适用于在
_Layout.cshtml中呈现的任何内容,以及由视图模板内的代码注入的任何内容但它不适用于视图模板中的硬编码字符串。
参考资料:
【问题讨论】:
标签: .net razor unicode servicestack