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.
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 callingGetPreamble()
.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.