How do we use CloudConfigurationManager with asp.net 5 JSON configs?

拟墨画扇 提交于 2019-11-29 03:25:09
tom.dietrich

Well, it turns out that when it comes to using CloudConfigurationManager with asp.net 5, the answer is that you don't, and the boilerplate code already covered it. (Thanks to Scott Hanselman for getting back to me on twitter)

So the standard approach is something like this:

IConfiguration configuration = new Configuration()
  .AddJsonFile("config.json") // adds settings from the config.json file
  .AddEnvironmentVariables(); // adds settings from the Azure WebSite config

The order in which these are called means that settings from the environment variables will over-write settings from the local config. All you have to do to make use of this is make sure the Azure settings will mimic your Json settings- so if your json file looks like

{
  "AppSettings": {
    "ConnectionString": "blahblahblah"
  }
}

You'd want to set up your setting in azure to look like

Key: AppSettings:ConnectionString 
Value: blahblahblah

, and then you can go ahead and use the exact same code you'd use for the local config.

var connectionString = Configuration.Get("AppSettings:ConnectionString");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!