最简单的方式

引用

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json

json文件

新建一个ConfigTest.json
右键属性,设置为“始终复制”
数据如下:

{
"TotalCount": 2,
"Students": [
{
"Name": "Lili",
"Sex": "女"
},
{
"Name": "Tom",
"Sex": "男"
}
]
}

读取json

var builder = new ConfigurationBuilder();
builder.AddJsonFile("ConfigTest.json");
var config = builder.Build();
Console.WriteLine(config["TotalCount"]);
//索引
Console.WriteLine(config["Students:0:Name"]);
Console.WriteLine(config["Students:0:Sex"]);
Console.WriteLine(config["Students:1:Name"]);
Console.WriteLine(config["Students:1:Sex"]);

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreConsole/Config/ConfigReadDemo.cs 的ReadConfig方法

Bind读取配置

引用

第一个示例中的引用也要添加

Microsoft.Extensions.Configuration.Binder

读取json

var builder = new ConfigurationBuilder()
.AddJsonFile("ConfigTest.json");
ConfigTest configTest=new ConfigTest();
var config = builder.Build();
//Microsoft.Extensions.Configuration.Binder
config.Bind(configTest);
Console.WriteLine(configTest.TotalCount);
Console.WriteLine(configTest.Students[0].Name);
Console.WriteLine(configTest.Students[0].Sex);
Console.WriteLine(configTest.Students[1].Name);
Console.WriteLine(configTest.Students[1].Sex);

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreConsole/Config/ConfigReadDemo.cs 的ReadConfigByBind方法

Option

引用

第一个示例中的引用也要添加

Microsoft.Extensions.Options

在appsettings.json中添加测试数据

{
"MyData": {
"TotalCount": 2,
"Students": [
{
"Name": "Lili",
"Sex": "女"
},
{
"Name": "Tom",
"Sex": "男"
}
]
}
}

实体类需要实现IOptions<T>

public class ConfigTest:IOptions<ConfigTest>
{
public int TotalCount { get; set; }
public List<Student> Students { get; set; }
public ConfigTest Value => this;
}

读取json

public class ConfigController : Controller
{
private ConfigTest _configTestByOptions;
private readonly IConfiguration _configuration;
public ConfigController(IConfiguration configuration)
{
_configuration = configuration;
}
public IActionResult Index()
{
//GetSection:必须先读节点
//ConfigTest实现接口IOptions<ConfigTest>
_configTestByOptions = _configuration.GetSection("MyData").Get<ConfigTest>();
return View();
}
}

注意

CreateDefaultBuilder方法已经读取appsettings.json,所以在这个示例中,我们没有调用AddJsonFile方法添加appsettings.json文件,而是直接读取appsettings.json中的数据即可

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreWebsite/Controllers/ConfigController.cs

热更新

定义

optional:该配置文件是否可选,如果optional=false,加载失败会抛出异常
reloadOnChange:文件被修改时是否重新加载,热更新就是这个,设置为true即可

public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange)

注意,热更新是修改bin目录下的生成的文件

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreConsole/Config/ConfigReadDemo.cs 的ReadConfigHotUpdate方法

.net core config读取的更多相关文章

  1. asp.net core mvc 读取appsettings.config中文乱码问题

    asp.net core mvc 读取appsettings.config中文乱码问题的解决方法如下: 用记事本打开appsettings.config,另存为的时候,编码设置为 “UTF-8”,

  2. NET Core开发-读取配置文件Configuration

    ASP.NET Core开发-读取配置文件Configuration   ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NE ...

  3. [.NET Core] 简单读取 json 配置文件

    简单读取 json 配置文件 背景 目前发现网上的 .NET Core 读取配置文件有点麻烦,自己想搞个简单点的. .NET Core 已经不使用之前的诸如 app.config 和 web.conf ...

  4. NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

    今天调试SSM框架项目后台JSOn接口,报出来一个让人迷惑的错误:NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config 上网查了一下别人的博 ...

  5. springMVC: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config

    springMVC开发web的时候,报错:java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 原因:未引入jstl ...

  6. Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config解决

    出现这个问题往往伴随  HTTP-500错误 报错信息: HTTP Status - Handler processing failed; nested exception is java.lang. ...

  7. javax/servlet/jsp/jstl/core/Config

    javax/servlet/jsp/jstl/core/Config springmvc出现的问题. 尝试了各种jar,问题依旧. DispatcherServlet配置如下. <bean id ...

  8. 解决NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

    使用spring3.05 mvc进行开发,使用tomcat容器,通过url映射寻找view的时候,会报错NoClassDefFoundError: javax/servlet/jsp/jstl/cor ...

  9. java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

    今天写SpringMvc时,遇到这样一个问题: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config at org.sp ...

随机推荐

  1. 【翻译自mos文章】Clusterware间歇性的hang,命令报CRS-184而且Network Socket Files in /tmp/.oracle or /var/tmp/.oracle被删

    来源于: Clusterware Intermittently Hangs And Commands Fail With CRS-184 as Network Socker Files in /tmp ...

  2. iOS开发之Quarz2D:九:图形上下文矩阵操作

    #import "VCView.h" @implementation VCView - (void)drawRect:(CGRect)rect { // Drawing code ...

  3. 【Nutch2.2.1基础教程之1】nutch相关异常 分类: H3_NUTCH 2014-08-08 21:46 1549人阅读 评论(2) 收藏

    1.在任务一开始运行,注入Url时即出现以下错误. InjectorJob: Injecting urlDir: urls InjectorJob: Using class org.apache.go ...

  4. [Ramda] Declaratively Map Data Transformations to Object Properties Using Ramda evolve

    We don't always control the data we need in our applications, and that means we often find ourselves ...

  5. [NPM] Run npm scripts in series

    After creating several npm script it becomes useful to run multiple scripts back-to-back in series. ...

  6. [React] Use React Context to Manage Application State Through Routes

    We’ll create a Router component that will wrap our application and manage all URL related state. We’ ...

  7. Redis学习笔记4-Redis配置具体解释

    在Redis中直接启动redis-server服务时, 採用的是默认的配置文件.採用redis-server   xxx.conf 这种方式能够依照指定的配置文件来执行Redis服务. 依照本Redi ...

  8. 将jar安装到本地mvn仓库

    声明:仅限于将maven Repository下载的jar(使用maven打包的jar)安装到本地的maven仓库中,不保证全部成功,最初的时候添加依赖发现下载始终不成功,只能手动下载,但是手动下载完 ...

  9. erlang 符号相关基本语法

    http://blog.csdn.net/anghlq/article/details/6803332 ErLang语法约定: 大写字母开头的名字(比如Address),表示一个变量,包括参数.局部变 ...

  10. 【t014】拯数

    [题目链接]:http://noi.qz5z.com/viewtask.asp?id=t014 [题意] [题解] 这个锁的序列,如果把末尾的0去掉; 然后再倒过来; 那么就是这个序列对应的格雷码了; ...