简体   繁体   English

.NET Core 中带有特殊字符的 appsettings

[英]appsettings with special characters in .NET Core

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 ó ).我有一个小的 .NET Core 控制台应用程序,它有一个包含不同配置部分的appsettings.json文件,其中一些值包含重音符号(例如áó )。

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?配置命名空间中是否有一个选项可以帮助正确解析字符串?

Real example:真实例子:

{
  "Loc": "https://www.mywebsite.com/image.jpg",
  "Caption": "Ocasión - Los mejores vehículos"
},

JSON mandates UTF-8 as the file encoding. JSON 要求 UTF-8 作为文件编码。 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.您的文件很可能以某种其他编码保存,可能是代码页 1252。确保将文件保存为 UTF-8,这样您的字符就可以使用了。

Different tools handle this differently.不同的工具对此有不同的处理方式。

For Notepad there's an Encoding selection in the Save dialog:对于记事本,保存对话框中有一个编码选择:

在此处输入图片说明

Visual Studio has a Save with Encoding option in the Save dialog: Visual Studio 在 Save 对话框中有一个Save with Encoding选项:

在此处输入图片说明

You could also write a small program or script to do the conversion, eg the following PowerShell pipeline:您还可以编写一个小程序或脚本来进行转换,例如以下 PowerShell 管道:

(Get-Content appsettings.json) | Set-Content -Encoding Utf8 appsettings.json

Inspired by Deep Dive into Microsoft Configuration , I found a solution.受到Deep Dive into Microsoft Configuration 的启发,我找到了一个解决方案。 My solution is to combine the use of json and xml.我的解决方案是结合使用json和xml。

In Your Program.cs, You need to add the load of xml.在您的 Program.cs 中,您需要添加 xml 的负载。 Example where I map settings to a POCO:我将设置映射到 POCO 的示例:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(AddDbConfiguration)
            .UseStartup<Startup>();

    private static void AddDbConfiguration(WebHostBuilderContext context, IConfigurationBuilder builder)
    {
        var configuration = builder.Build();
        builder.AddXmlFile("appsettings.xml");
    }

My xml file:我的 xml 文件:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <ConfigSettings>
    <Database>Specialskolekørsel</Database>
    <SystemId>1</SystemId>
    <EnableAudit>True</EnableAudit>
    </ConfigSettings>
</root>

My ConfigureServices:我的配置服务:

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
   services.Configure<ConfigSettings>(Configuration.GetSection(nameof(ConfigSettings)));
}

My Controller:我的控制器:

public HomeController(IOptions<ConfigSettings> config)
{
    Database = config.Value.Database;
}

Now the danish letter ø shows as expected.现在丹麦字母 ø 按预期显示。 I hope You will find this useful.我希望你会发现这很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM