Core 读取配置文件
新建控制台
static void Main(string[] args)
{
Console.WriteLine("Hello World!"); //获取应用程序的当前工作目录 包含当前工作目录路径的字符串,但不包含//以反斜杠(\)结束。
var pathToContentRoot = Directory.GetCurrentDirectory();
//用于构建基于键/值的配置设置,以便在应用程序中使用
var builder = new ConfigurationBuilder()
.SetBasePath(pathToContentRoot)//将基于文件的提供程序的FileProvider设置为PhysicalFileProvider基本路径
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)//在构建器的路径中添加JSON配置提供程序
.AddEnvironmentVariables();//添加读取的Microsoft.Extensions.Configuration.IConfigurationProvider来自环境变量的配置值
Coin.Service.Models.CoinAppSettings.CreateInstence(builder.Build());// 获取具有指定键的配置子节 这边开始读取配置文件了!!! Console.WriteLine("Hello World!");
Console.ReadKey();
}
CreateInstence 这个方法的实现 及 CoinAppSettings这个类库的代码如下
public class CoinAppSettings
{
public DbConnection ConnectionStrings { get; }
public AppSettings AppSettings { get; }
public static CoinAppSettings Instance { get; private set; }
public static void CreateInstence(IConfigurationRoot builder)
{
Instance = new CoinAppSettings(builder);
}
public CoinAppSettings(IConfigurationRoot builder)
{
this.ConnectionStrings = new DbConnection(builder.GetSection("ConnectionStrings"));
this.AppSettings = new AppSettings(builder.GetSection("AppSettings"));
}
}
public class AppSettings
{
public string ApiHost { get; }
public AppSettings(IConfigurationSection section)
{
this.ApiHost = section.GetSection("ApiHost").Value;
}
}
Core 读取配置文件的更多相关文章
- .NET Core 读取配置文件方式总结
基于.NET Core的跨平台开发,配置文件与之前.NET Framework采用xml的config文件不同,目前主要是采用json文件键值对配置方式读取. 参考网上相关资料总结如下: 一.引入扩展 ...
- .net core 读取配置文件的值
.net core中的配置文件可以存一些自定义的值,我们需要去读取 在配置中添加json: "name": "sealee", "Connection ...
- ASP .NET CORE 读取配置文件的方法
老式的config文件在ASP.net core2.0中变成了appsettings.json,如果想要读取自定义配置,可以写如下代码 { "Logging": { "I ...
- .net core 读取配置文件
/// <summary> /// 读取配置信息 /// </summary> public class Zconfig { #region 读取配置信息 /// <su ...
- .Net Core 读取配置文件 appsettings.json
1. 首先些一个类 public class MySettings { public string P1 { get; set; } public string P2 { get; set; } } ...
- Asp.net Core 和类库读取配置文件信息
Asp.net Core 和类库读取配置文件信息 看干货请移步至.net core 读取配置文件公共类 首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取 ...
- .net core 学习 读取配置文件
在空项目中是没有配置文件的,首先要新建一个,配置文件内容如下,下面来读取各个内容 { "ConnectionStrings": { "DefaultConnection& ...
- .net core 读取appsettings.json乱码
.net core 读取配置文件乱码:vs2019读取appsettings.json乱码问题; .net core 读取appsettings.json乱码问题;用notepad++或者其他编辑器打 ...
- 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...
随机推荐
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
ES5中,一共有9个Array方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.pr ...
- windows下运行Eigen
参看:https://blog.csdn.net/zhujiahui622/article/details/50504678## 下载C++下的一个线性代数运算库Eigen并解压:http://eig ...
- java数据结构至对称矩阵压缩存储
刚刚刷java选择题,遇到的对称矩阵压缩存储问题,我们知道对称矩阵是aij=aji的矩阵,压缩存储可以采用一维数组和二维数组存储. 此处只讨论一维数组存储的形式,设数组下标从0开始,对称矩阵为n维矩阵 ...
- E:could not get lock /var/lib/dpkg/lock -ope
最近研究ubuntu,用apt-get命令安装一些软件包时,总报错:E:could not get lock /var/lib/dpkg/lock -open等 出现这个问题的原因可能是有另外一个程序 ...
- linux中tomcat startup.sh出现commond not found
问题: 前些天,再Linux提交更新代码启动tomcat时报commond not found 过程: 查了下百度,http://code2care.org/2015/-bash:-startup.s ...
- Koa源码分析(三) -- middleware机制的实现
Abstract 本系列是关于Koa框架的文章,目前关注版本是Koa v1.主要分为以下几个方面: Koa源码分析(一) -- generator Koa源码分析(二) -- co的实现 Koa源码分 ...
- Springboot+Mybatis+Thymeleaf
工具Eclipse 2018 Maven 配置 ThymeLeaf 安装: https://blog.csdn.net/xingqibaing/article/details/82787164 sp ...
- python 环境变量设置PYTHONPATH
PYTHONPATH是Python搜索路径,默认我们import的模块都会从PYTHONPATH里面寻找. 打印PYTHONPATH: import os print sys.path >['' ...
- weblogic获取应用目录路径(war包)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- Yaf 完全精通
bugs 这样 _Bootstrap 的话,会导致严重的后果,cpu 100%