我们如何在新的asp.net vnext中存储配置数据? [英] How can we store configuration data in new asp.net vnext?
问题描述
我们如何在新的asp.net vnext中存储配置数据? web.config仍然存在(删除了system.web,因此没有任何web.config,但是我喜欢)或使用新的json文件存储配置数据.
How can we store configuration data in new asp.net vnext? web.config still there(system.web removed, so no any web.config,but I like it) or using new json file for configuration data.
推荐答案
不知道您要存储的是哪种数据,但这对我来说确实有用.我创建了此文件myconfig.json并将所需的数据以JSON格式存储.
Not sure exactly what kind of data you are trying to store but this work for me. I created this file myconfig.json and store that data that I need in a JSON format.
这将注册您的配置文件,这是我在startup.cs页面中添加的代码.
This will register your configuration file, I added this code in the startup.cs page.
Configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile("myconfig.json")
.AddEnvironmentVariables();
要从JSON文件中获取所需的数据,您需要在代码上的任何时候做到这一点.
To get the data that you need from the your JSON file you need to this at any point on your code.
IConfiguration Configuration = new Configuration().AddJsonFile("myconfig.json");
var jsonNodes = Configuration.Get("SomeJsonNode");
string someJsonString = Configuration.Get("someJsonString");
仅供参考:目前,我测试不支持代码json数组.不知道他们是否现在就解决了.
FYI: At the moment that I tested that code json array were not supported. Not sure if they fixed that now.
这篇关于我们如何在新的asp.net vnext中存储配置数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!