Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

charset utf-8 in asp.net for special turkish characters

I put <meta http-equiv="content-type" content="text/html; charset=utf-8" /> in the head, and DOCTYPE at the top of my _Layout.cshtml, but still when I'm viewing the website, the special Turkish characters are displayed as like &#23'1; and such in the source, not in the page. The webpage displays them correctly, the source file has the problem.. Do you know what else I should do to correct this?

like image 992
Halo Avatar asked Dec 21 '22 13:12

Halo


2 Answers

Try to save your source file as UTF-8 with BOM.
I was having a similar issue and it seems that Razor expects files to have the BOM signature.

In Visual Studio: FILE > Advanced Save Options:

Advanced Save Options dialog

like image 67
Renaud Bompuis Avatar answered Feb 18 '23 06:02

Renaud Bompuis


I had exactly the same problem, tried all the solutions and none of them worked, checked .cshtml file encoding with Notepad++ and it was correct (utf8 with BOM), SaveAs file with encoding (as shown above) but the problem still persisted.

Finally I found out that there was nothing wrong with my razor files, it seems that ASP.Net core MVC does not set UTF-8 encoding as default encoding and it must be set if needed by developer.

I added the below code at the end of ConfigureServices method of Program.cs (it adds UTF-8 encoding to default encodings) and the problem solved.

services.AddWebEncoders(o => {
            o.TextEncoderSettings = new System.Text.Encodings.Web.TextEncoderSettings(UnicodeRanges.All);
        });
like image 37
Code_Worm Avatar answered Feb 18 '23 06:02

Code_Worm