如何读取ASP.NET vNext从Config.json的AppSettings值

编程入门 行业动态 更新时间:2024-10-24 06:30:51
本文介绍了如何读取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值

本文发布于:2023-11-02 14:59:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:vNext   NET   ASP   AppSettings   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!