如何读取ASP.NET vNext从Config.json的AppSettings值 [英] How to read AppSettings values from Config.json in ASP.NET vNext

查看:193
本文介绍了如何读取ASP.NET vNext从Config.json的AppSettings值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我的AppSettings设置在Config.json是这样的:

I have got my AppSettings setup in Config.json like this:

{
  "AppSettings": {
        "token": "1234"
    }
}

我在网上搜索如何读取config.json的AppSettings值,但我无法得到任何有用的东西。

I have searched online on how to read AppSettings values from config.json but I could not get anything useful.

我试过:

var configuration = new Configuration();
var appSettings = configuration.Get("AppSettings"); // null
var token = configuration.Get("token"); // null

我知道与ASP.NET 4.0,你可以这样做:

I know with ASP.NET 4.0 you can do this:

System.Configuration.ConfigurationManager.AppSettings["token"];

但我怎么做到这一点的ASP.NET vNext?

But how do I do this in ASP.NET vNext?

推荐答案

您必须首先注册您的配置文件:

You first have to register your config file:

public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
     // Setup configuration sources.
     Configuration = new Configuration(appEnv.ApplicationBasePath)
     .AddJsonFile("config.json")
     .AddEnvironmentVariables();
}

然后查询数据:

var token = configuration.Get<string>("AppSettings:token");

您需要的依赖添加到您的 project.config 文件:

You'll need to add a dependency to your project.config file:

"dependencies": {
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta7",
}

和获得的NuGet包

这篇关于如何读取ASP.NET vNext从Config.json的AppSettings值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆