如何在同一appsettings.json文件中引用另一个值? [英] How to reference another value within the same appsettings.json file?
本文介绍了如何在同一appsettings.json文件中引用另一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在appsettings.json中的两个地方添加数据库连接字符串.
I need database connection string in two places in appsettings.json.
是否可以在json文件中引入与变量或json路径相关的公共引用,以避免潜在的问题?
Is it possible to introduce common variable or json-path related references into json file to avoid potencial problems?
不用触摸c#代码就能拥有它.
It would be lovely to have it without touching c# code.
{
...
"ConnectionStrings": {
"Default": "Host=localhost;Database=db;Port=5432;Username=postgres;Password=postgres"
},
"Nlog": {
"targets": {
"database": {
"type": "Database",
"dbProvider": "Npgsql.NpgsqlConnection, Npgsql",
"connectionString": "Host=localhost;Database=db;Port=5432;Username=postgres;Password=postgres",
...
}
}
...
}
推荐答案
NLog可以在appsettings.json中查找值.您可以使用${configsetting}
这样完成:
NLog has the ability to lookup values in the appsettings.json. You can do it like this with ${configsetting}
:
{
...
"ConnectionStrings": {
"Default": "Host=localhost;Database=db;Port=5432;Username=postgres;Password=postgres"
},
"Nlog": {
"targets": {
"database": {
"type": "Database",
"dbProvider": "Npgsql.NpgsqlConnection, Npgsql",
"connectionString": "${configsetting:item=ConnectionStrings.Default}",
...
}
}
...
}
另请参见 https://github.com/NLog/NLog/wiki /ConfigSetting-Layout-Renderer
这篇关于如何在同一appsettings.json文件中引用另一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文