1

Trying to learn Visual Studio 2015 MVC and looking for some advice on how to retrieve multiple config values from config.json.

Currently I have the following:

public IEnumerable<WeatherConfigValues> WeatherAppSetting()
        {
            var con = new Configuration();
            con.AddJsonFile("Config.json");
            var weatherConfigSettings0 = con.Get("AppSettings:WeatherKey");
            var weatherConfigSettings1 = con.Get("AppSettings:Weather_FeedKey");

            var weatherConfigSettings = new List<WeatherConfigValues>
            {
                new WeatherConfigValues {WeatherKey         = weatherConfigSettings0},
                new WeatherConfigValues {WeatherFeedKey     = weatherConfigSettings1}
            };

            return weatherConfigSettings.ToList();
        }

Just ignore the names as I'm just trying to get code to work.

The above code does work and I can get the values in another class Library with the following:

var weatherSettings = new CustomConfigurationSettings();
            var getData         = weatherSettings.WeatherAppSetting().ToList();
            var test1           = getData[0].WeatherKey;
            var test2           = getData[1].WeatherFeedKey;

I have been looking at numerous websites such as http://blog.jsinh.in/asp-net-5-configuration-microsoft-framework-configurationmodel/#.VVYkyPlVhBd and http://whereslou.com/2014/05/23/asp-net-vnext-moving-parts-iconfiguration/ but I'm not sure if I'm doing it the correct way, I tried GetSubKeys but that just returns null values.

So my question is, is this the correct way, or is there a correct way.

svick
  • 236,525
  • 50
  • 385
  • 514
George Phillipson
  • 830
  • 11
  • 39
  • Why do you think this might not be the correct way? – svick May 16 '15 at 12:32
  • @svick just seems a long way round to pull the data from config.json, thought there might be another way – George Phillipson May 16 '15 at 17:04
  • The way you're representing the data is indeed overcomplicated (and unsafe). Is there any reason why you can't use just `return new WeatherConfigValues { WeatherKey = con.Get("AppSettings:WeatherKey"), WeatherFeedKey = con.Get("AppSettings:Weather_FeedKey") };`? – svick May 16 '15 at 17:53

0 Answers0