【问题标题】:Accented characters in Views are not rendered properly视图中的重音字符未正确呈现
【发布时间】: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


    【解决方案1】:

    好的,回答我自己的问题,以防有人偶然发现类似问题:

    • Razor 似乎要求所有文件都是 UTF8 带有 BOM。

    我显然被其中一个参考资料误导了,但找到了an answer to a similar question

    它现在可以正常工作了。

    【讨论】:

    • 在相关说明中,这是向所有 razor 文件添加 BOM 的方式:` sed -i '1s/^(\xef\xbb\xbf)\?/\xef\xbb\ xbf/' *.cshtml`。它处理带有和不带有 bom 的文件。
    猜你喜欢
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多