Problems solving

Encoding UTF-8 in .cshtml files

Some time ago I was struggling with displaying Polish characters in “*.cshtml” files of my newest ASP.NET Core 2.1 project. Solution is not so obvious, so I decided to write a few words about it and give you a quick fix.

If you are here because of some search engine results you probably know that this in your <head></head> tags:
<meta charset="UTF-8" />
doesn’t work and after running your program you still can’t see your special characters.

It’s because your Visual Studio doesn’t generated those *.cshtml files in UTF-8. And the solution for this problem is simple, but not so easy to come up with. So here it is:

After first completed build of your solution, find your *.cshtml files in this path:

C:\Users\[YOUR_USER_NAME]\.nuget\packages\Microsoft.VisualStudio.Web.CodeGenerators.Mvc\[VERSION OF YOUR MVC E.G 2.0.1]\Templates\

There you can find some folders with your generated files. Open all of the files with Notepad++ (or any other editor where you can set encoding) , convert every file to UTF-8 and save them.

And here you are! 😉 Now with each build, your website will correctly display the UTF-8 characters.

Leave a Reply