英文:
Access Azure Application Settings using IConfiguration
问题
I have a setting in my Application settings on my Azure Web App set up like so:
I'm trying to access it in the code like so: configuration["AppSettings:LASTFM_API_KEY"]
This works when I have the secret set up in my secrets.json file like so:
But not when I publish my app to Azure. I first tried it in Azure as LASTFM_API_KEY
and then tried adding AppSettings__
, neither worked.
How can I access this secret when I publish my app? Is it possible to access it using configuration in the same way both locally and when published to Azure?
英文:
I have a setting in my Application settings on my Azure Web App set up like so:
I'm trying to access it in the code like so: configuration["AppSettings:LASTFM_API_KEY"]
This works when I have the secret set up in my secrets.json file like so:
But not when I publish my app to Azure. I first tried it in Azure as LASTFM_API_KEY
and then tried adding AppSettings__
, neither worked.
How can I access this secret when I publish my app? Is it possible to access it using configuration in the same way both locally and when published to Azure?
答案1
得分: 0
我错过了AddEnvironmentVariables()
的调用,而且这也需要在可能用另一个文件中的类似值覆盖该值的任何调用之后进行调用(比如appsettings.json)。我把它放在最后,像这样:
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddUserSecrets(Assembly.GetEntryAssembly()!, true)
.AddEnvironmentVariables()
.Build();
英文:
I was missing the AddEnvironmentVariables()
call, and also this needs to be called after any calls that might overwrite the value with a similar value in another file (like appsettings.json). I put it last like so:
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddUserSecrets(Assembly.GetEntryAssembly()!, true)
.AddEnvironmentVariables()
.Build();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论