12

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.

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.

JabberwockyDecompiler
  • 3,318
  • 2
  • 42
  • 54
user3779544
  • 121
  • 3
  • Could you not simply have a build step that checks the cshtml for BOMs, and adds them if missing? – Marc Gravell Jun 26 '14 at 13:37
  • @MarcGravell CSHTML files are not part of the build step, they're compiled dynamically. Usually way is edit the file and refresh the browser. No build step involved. – user3779544 Jun 26 '14 at 13:43
  • 3
    so whatever process it is that you use to deploy/distribute cshtml; could it be a {that} step? – Marc Gravell Jun 26 '14 at 14:02
  • @MarcGravell I guess it would be possible to add a custom step there and modify all CSHTML files, but that's really not a nice solution either (same as re-creating the `VirtualPathProvider`). The frontend developer edit the file, save it and refresh the browser. There is not even a deploy/distribute step here. It would be much better to make Razor understand that UTF-8 files without a BOM are still UTF-8, hence my question. – user3779544 Jun 26 '14 at 14:25
  • 3
    of course here's another radical idea: give your front-end developers tools that save the file in an appropriate format. Crazy talk, I know, but still... – Marc Gravell Jun 26 '14 at 14:38
  • 2
    I'm sorry to say, but you're not really helping. Of course it's possible to make them add the mark, but I'm trying to look for an easier solution that makes more sense - to have it centralized and to make Razor not require this mark. It's easier to have this at one central station instead of making every developer change the settings of their editor of choice (not everyone uses the same) - which also would feel quite unnatural to them, because commonly all files are without this mark (that is not even required for UTF-8). – user3779544 Jun 26 '14 at 14:51
  • What about the day one of them saves the file in ISO-8859-1 ? Or in UTF-16 ? Will you force UTF-8 then, also ? What if it happens in 2 years and you're not at this company anymore, how will your successor find about this "fix" ? – thomasb Aug 13 '15 at 15:50

1 Answers1

10

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>
jkolo
  • 116
  • 1
  • 5