Search code examples
asp.net-corerazor

Single quotation character in Razor cshtml not recognized as a valid character after converting site to asp.net core


I have converted my previous asp.net MVC .net framework website to .net core 6 but am experiencing a strange issue.

The single quotation symbol i.e. `` that I have embedded in text in my cshtml is showing up as a character with the unicode replacement character quotation mark in a diamond i.e. � in my runtime output

At runtime my cshtml has the unicode replacement character in it, in the editor it shows the accent character as correct.

Example

However the exact cshtml file shows correct in the old asp.net 4

This is not a Windows setting issue as the old site displays correctly on the same machine What I have tried.

I tested various meta headers to to the page header

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

and

<meta http-equiv="Content-Type" content="text/html; charset=UTF-32" />

In my Program.cs I tried adding

 builder.Services.Configure<WebEncoderOptions>(options =>
{
    options.TextEncoderSettings = new 
    TextEncoderSettings(UnicodeRanges.All);
});

and then tried

builder.Services.AddSingleton<HtmlEncoder>(
        HtmlEncoder.Create(allowedRanges: new[] { UnicodeRanges.All 
}));

also tried

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

in my web.config

but output is still the same ith the question mark character at runtime


Solution

  • I found the problem. Open the cshtml file location in windows explorer and open the file in notepad. Then save as and I saw the default encoding is ANSI. I then save the file as UTF-8.

    screenshot

    What caught me out is it shows correctly in Visual studio and notepad but only incorrectly at runtime so I thought the file encoding is something in asp.net