简体   繁体   English

如何从ASP.NET 5中的config.json检索值

[英]How to retrieve value from config.json in ASP.NET 5

I am facing issue with visual studio 2015 express addition.I have a console application and I wanted to keep some value in configuration file but with VS2015 there is no option available to add a App.config file instead of that it's giving me to add config.json file. 我遇到了Visual Studio 2015 Express Adding的问题。我有一个控制台应用程序,我想在配置文件中保留一些价值,但是在VS2015中,没有可用的选项来添加App.config文件,而是给我添加配置.json文件。

Now I don't have any idea how to keep and retrieve value inside the json.config. 现在我不知道如何在json.config中保存和检索值。

With the previous version of VS2015, it was very simple all I need to do was to create the object of Configuration manager and call respective method. 在VS2015的先前版本中,我要做的就是非常简单,即创建配置管理器的对象并调用相应的方法。 I don't know why they removed this features from VS2015. 我不知道为什么他们从VS2015中删除了此功能。

Please give me resolution on this. 请给我解决办法。

With ASP.NET 5 configuration is moved to json files. 随着ASP.NET 5的配置被移动到json文件。 You can find some info here: http://www.mikesdotnetting.com/article/284/asp-net-5-configuration http://whereslou.com/2014/05/23/asp-net-vnext-moving-parts-iconfiguration/ 您可以在此处找到一些信息: http : //www.mikesdotnetting.com/article/284/asp-net-5-configuration http://whereslou.com/2014/05/23/asp-net-vnext-moving-零件-i配置/

Below constructor: Below example will read value "somevalue" for property "OuterKey:InnerKey" from config.json file. 在构造函数下:下面的示例将从config.json文件中读取属性“ OuterKey:InnerKey”的值“ somevalue”。

Constructor for Program: 程序的构造函数:

public void Program()
{
    var configurationBuilder = new ConfigurationBuilder()
       .AddJsonFile("config.json")
       .AddEnvironmentVariables();
    var myConfiguration = configurationBuilder.Build();
    string value = myConfiguration["OuterKey:InnerKey"];
}

Config.json: Config.json:

 "OuterKey": {
      "InnerKey": "somevalue"
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM