MVC 3 Razor page encoded as utf 8 shows encoded ch

2019-05-06 12:56发布

问题:

I have a VirtualPathProvider which takes source code from my DB as a plain string and encodes it as UTF8.

For example:

public override Stream Open()
{
  MemoryStream result = new MemoryStream();

  result = new MemoryStream(Encoding.UTF8.GetBytes(_sourceCode));

  return result;
}

I then have a layout master page which has its charset as UTF 8

<meta charset="utf-8">

The master page then calls @RenderBody() which gets my VirtualPathProvider page and outputs it to the browser.

The problem is that it is outputting the page with its encoded characters:

wünschte becomes wünschte

What am I doing wrong?

TLDR:

I want wünschte to display instead of wünschte. The plain string from the DB is wünschte, but once it comes from the memory stream onto my page it becomes wünschte.

回答1:

As someone who struggled with this today with his own VirtualPathProvider implementation, it turns out that Razor really wants the byte order mark. You can force this by calling GetPreamble().

using System.Linq; // for Concat() because I am lazy

public override Stream Open()
{
    var template = Encoding.UTF8
        .GetBytes(this.Template);
    var buffer = Encoding.UTF8
        .GetPreamble()
        .Concat(template)
        .ToArray();
    return new MemoryStream(buffer);
}

If the BOM isn't there, Razor seems to default to the current codepage and not UTF8. The above fixed it for me.

In the example above, you'd replace this.Template in my implementation with _sourceCode in the original question.