MVC 3 Razor page encoded as utf 8 shows encoded characters

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.

Answers


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.


Need Your Help

MySQL Left JOIN Group_concat Performance issues

php mysql sql left-join group-concat

Please I need a help. I have a query I run to fetch data from two tables, but its consuming a lot of memory it takes as much as 9 seconds to fetch about 100 records. I may need to fetch up to 200k

Java: Find Height of N-ary Tree

java tree height

There's lots on finding the height of binary trees; how can the height of a n-ary tree be found in Java? Would it involve traversing it and keeping track of max depth or something like that?