如何使Razor读取没有BOM的UTF

如何使Razor读取没有BOM的UTF

本文介绍了如何使Razor读取没有BOM的UTF-8文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将前端和后端工作分为不同的团队.前端使用了大量的编辑器来编辑 CSHTML ,其中大多数都保存了 UTF-8 而没有字节顺序标记.问题是,Razor期望存在 BOM .如果不存在,它将使用当前代码页读取文件,并且我们遇到编码问题.

We have separated teams for front end and back end work. The front end is using a large diversity of editors to edit the CSHTML and most of them save UTF-8 without a byte order mark. The problem is, Razor expects a BOM to be present. If it's not present it will read the file using the current code page and we get encoding problems.

我如何让Razor接受没有 BOM UTF-8 文件?显然,唯一的解决方案是实现自己的 VirtualPathProvider ,从而实现一个 VirtualFile .不幸的是,当前使用的 VirtualPathProvider MapPathBasedVirtualPathProvider ,它是一个内部类.因此,我不得不重新创建很多功能.

How would I make Razor to accept the UTF-8 files without a BOM? Apparently the only solution would be to implement an own VirtualPathProvider and thus also a VirtualFile. Unfortunately, the current used VirtualPathProvider is MapPathBasedVirtualPathProvider, which is an internal class. So I'd have to re-create a lot of functionality.

推荐答案

跟随,请尝试将其添加到 Web.config 中:

Following How to force ASP.NET MVC to read .cshtml files as UTF-8?, try to add this in Web.config:

<system.web>
    <globalization fileEncoding="utf-8" />
</system.web>

这篇关于如何使Razor读取没有BOM的UTF-8文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:02