1

尝试学习 Visual Studio 2015 MVC 并寻找有关如何从 config.json 检索多个配置值的建议。

目前我有以下内容:

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();
        }

只需忽略名称,因为我只是想让代码正常工作。

上面的代码确实有效,我可以使用以下内容在另一个类库中获取值:

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

我一直在查看许多网站,例如http://blog.jsinh.in/asp-net-5-configuration-microsoft-framework-configurationmodel/#.VVYkyPlVhBdhttp://whereslou.com/2014/05/23 /asp-net-vnext-moving-parts-iconfiguration/ 但我不确定我是否以正确的方式进行操作,我尝试了 GetSubKeys但它只返回空值。

所以我的问题是,这是正确的方法,还是有正确的方法。

4

0 回答 0