How to reference another value within the same appsettings.json file?

前端 未结 3 1851
一个人的身影
一个人的身影 2021-01-24 00:13

I need database connection string in two places in appsettings.json.

Is it possible to introduce common variable or json-path related references into json file to avoid

3条回答
  •  无人共我
    2021-01-24 00:37

    I've created a nuget package exactly for this! Check it out here: https://www.nuget.org/packages/TemplateFormattedConfiguration/

    In your example you should do this:

    {
    ...
        "ConnectionStrings": {
    "Default": "Host=localhost;Database=db;Port=5432;Username=postgres;Password=postgres"
      },
      "Nlog": {
        "targets": {
          "database": {
            "type": "Database",
            "dbProvider": "Npgsql.NpgsqlConnection, Npgsql",
            "connectionString": "{ConnectionStrings:Default}",
    ...
          }
        }
    ...
    }
    

    And in your Startup.cs (or Program.cs) you'll add this:

            configuration.EnableTemplatedConfiguration();
    

提交回复
热议问题