在 Azure 函数 v1 中,我尝试使用 GetEnvironmentVariable
静态方法读取 local.settings.json
中存在的连接字符串
这是我的local.settings.json
文件
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
},
"ConnectionStrings": {
"OnPremisesConnection": "Server=test;Initial Catalog=testdb;Integrated Security=SSPI;"
}
}
使用以下代码读取连接字符串
string variableName = "OnPremisesConnection";
var res = Environment.GetEnvironmentVariable($"ConnectionStrings:{variableName}")
但结果是NULL
。我在这里缺少什么?
最佳答案
好的。您可以尝试下一个:
var config = new ConfigurationBuilder()
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
string connectionStr = config["ConnectionStrings:OnPremisesConnection"]
关于azure - 在 Azure 中使用 GetEnvironmentVariable 读取 local.settings.json 中的 ConnectionStrings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51786885/