.NET Core中带有特殊字符的appsettings [英] appsettings with special characters in .NET Core
问题描述
我有一个小型的.NET Core控制台应用程序,其中有一个 appsettings.json
文件,其中包含不同的配置部分,并且某些值包含重音符号(例如á
或ó
)。
I have a small .NET Core console app which has an appsettings.json
file containing different configuration sections and some of the values contain accents (for example á
or ó
).
当我解析给定的配置节时
When I parse a given configuration section I get weird chars back for those.
配置名称空间中是否有一个选项可以帮助正确解析字符串?
Is there an option in the configuration namespace that could help in parsing the string correctly?
实际示例:
{
"Loc": "https://www.mywebsite.com/image.jpg",
"Caption": "Ocasión - Los mejores vehículos"
},
推荐答案
JSON强制将UTF-8作为文件编码。您的文件很可能以其他编码方式保存,可能是Codepage1252。请确保将文件另存为UTF-8,并且字符可以正常工作。
JSON mandates UTF-8 as the file encoding. Your file is most likely saved in some other encoding, possibly Codepage 1252. Make sure you save the file as UTF-8 and your characters will work.
不同的工具可以处理此问题
Different tools handle this differently.
对于记事本,在保存对话框中有一个编码 选择:
For Notepad there's an Encoding selection in the Save dialog:
Visual Studio在保存对话框中有一个带编码保存 选项:
Visual Studio has a Save with Encoding option in the Save dialog:
您还可以编写一个小程序或脚本来进行转换,例如以下PowerShell管道:
You could also write a small program or script to do the conversion, e.g. the following PowerShell pipeline:
(Get-Content appsettings.json) | Set-Content -Encoding Utf8 appsettings.json
这篇关于.NET Core中带有特殊字符的appsettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!