.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 作为文件编码.您的文件很可能以某种其他编码保存,可能是代码页 1252.请确保将文件保存为 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屋!