.net core config读取
最简单的方式
引用
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();
示例代码
热更新
定义
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读取的更多相关文章
- asp.net core mvc 读取appsettings.config中文乱码问题
asp.net core mvc 读取appsettings.config中文乱码问题的解决方法如下: 用记事本打开appsettings.config,另存为的时候,编码设置为 “UTF-8”,
- NET Core开发-读取配置文件Configuration
ASP.NET Core开发-读取配置文件Configuration ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NE ...
- [.NET Core] 简单读取 json 配置文件
简单读取 json 配置文件 背景 目前发现网上的 .NET Core 读取配置文件有点麻烦,自己想搞个简单点的. .NET Core 已经不使用之前的诸如 app.config 和 web.conf ...
- NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
今天调试SSM框架项目后台JSOn接口,报出来一个让人迷惑的错误:NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config 上网查了一下别人的博 ...
- springMVC: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
springMVC开发web的时候,报错:java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 原因:未引入jstl ...
- 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. ...
- javax/servlet/jsp/jstl/core/Config
javax/servlet/jsp/jstl/core/Config springmvc出现的问题. 尝试了各种jar,问题依旧. DispatcherServlet配置如下. <bean id ...
- 解决NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
使用spring3.05 mvc进行开发,使用tomcat容器,通过url映射寻找view的时候,会报错NoClassDefFoundError: javax/servlet/jsp/jstl/cor ...
- java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
今天写SpringMvc时,遇到这样一个问题: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config at org.sp ...
随机推荐
- [CSS] Conditionally Apply Styles Using Feature Queries @supports
While browsers do a great job of ignoring styles they don’t understand, it can be useful to provide ...
- 五一巨献,问答有礼,105QB送给IT互联网界的劳动人民
活动主题:五一巨献,问答有礼,105QB送给IT互联网界的劳动人民活动时间:4月30日晚上10点~5月2日晚上10点活动期数:第1期,20150401 奖品:105QB获奖人数:20人1~5:每人10 ...
- ASCII,Unicode和UTF-8终于找到一个能完全搞清楚的文章了
前言 平时喜欢写东西,看博客,一直对编码有些懵,今天下午也不知道看到了什么,突然想了解下,就找到了这个文章,看完真的豁然开朗,这个必须留下来做纪念. 点击打开链接 1.ASCII 我们知道,计算机内部 ...
- 【hdu 3032】Nim or not Nim?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- amazeui中的js插件有哪些(详解功能)
amazeui中的js插件有哪些(详解功能) 一.总结 一句话总结: 二.amazeui中的js插件有哪些 1.UI 增强 警告框Alert 按钮交互Button 折叠面板Collapse 下拉组件D ...
- Erlang Process input queue
http://www.cnblogs.com/me-sa/archive/2011/11/05/erlang0012.html Erlang进程有自己的消息队列来保存接收到的消息,新接收到的消息放在队 ...
- 图片拉伸:IOS开发UIImage中stretchableImageWithLeftCapWidth
意思就是用来创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的宽度,第二个参数是上面不拉伸的高度.那么接下来的一个像素会被拉伸.例如,leftCapHeight为6,top ...
- WPF入门(三)->几何图形之不规则图形(PathGeometry)
原文:WPF入门(三)->几何图形之不规则图形(PathGeometry) 前面我们给大家介绍了LineGeometry,EllipseGeometry,CombinedGeometry等一些规 ...
- 一起学Python: 多线程-共享全局变量问题
多线程-共享全局变量问题 多线程开发可能遇到的问题 假设两个线程t1和t2都要对全局变量g_num(默认是0)进行加1运算,t1和t2都各对g_num加10次,g_num的最终的结果应该为20. 但是 ...
- matlab 高级函数 —— colfilt/blockproc (图像)矩阵的分块处理
colfilt 执行功能与 blockproc/nlfilter 类似,但效率更高. B = colfilt(A,[m n],block_type,fun),block_type:distinct/s ...