在空项目中是没有配置文件的,首先要新建一个,配置文件内容如下,下面来读取各个内容

{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true",
},
"Name": "张三",
"Person": {
"Name": "张三",
"Age":
},
"Persons": [
{
"Name": "张三",
"Age": 18
},
{
"Name": "李四",
"Age": 18
}
]
}

有时会出现中文读取乱码,用计事本打开,然后另存为 utf-8 就可以了

ConfigurationBuilder configurationBuilder =new ConfigurationBuilder();
//配置文件路径
configurationBuilder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
var configuration = configurationBuilder.Build(); //读取连接字符串
var value1= configuration.GetConnectionString("DefaultConnection"); //读取自定义内容
var value2 = configuration.GetValue<string>("Name");
var value3 = configuration.GetValue<string>("Person:Name");
var value4 = configuration.GetValue<int>("Person:Age");
var value5 = configuration.GetValue<string>("Persons:0:Name");
var value6 = configuration.GetValue<int>("Persons:0:Age");

索引不是中括号感觉有点不适应

上面是读取基础值,其实还可以将 Person 和 Persons 读取为对象,不过写法要改下,首先再 Startup 类中添加一个构造函数和字段

public Startup(IConfiguration _configuration)
{
Configuration = _configuration;
} public IConfiguration Configuration;

然后在 ConfigureServices 方法添加如下

services.Configure<Person>(Configuration.GetSection("Person"));
services.Configure<List<Person>>(Configuration.GetSection("Persons"));

之后是控制器中

private Person person;
private List<Person> persons;
public ConfigurationController(IOptions<Person> _person, IOptions<List<Person>> _persons)
{
person = _person.Value;
persons = _persons.Value;
}

以上就是 asp.net core 读取配置文件的方法

.net core 学习 读取配置文件的更多相关文章

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

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

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

    ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...

  3. ASP.NET Core开发-读取配置文件Configuration appsettings.json

    https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...

  4. python学习读取配置文件

    配置文件作为一种可读性很好的格式,非常适用于存储程序中的配置数据. 在每个配置文件中,配置数据会被分组(比如“config”和 “cmd”). 每个分组在其中指定对应的各个变量值.如下: # 定义co ...

  5. .net core自定义读取配置文件

    新建完成后项目目录下有个 appsettings.json { "Logging": { "LogLevel": { "Default": ...

  6. unittest(12)- 学习读取配置文件

    1.配置文件格式 2.读取配置文件 import configparser """ 通过读取配置文件,来执行相应的测试用例 配置文件分为2个部分 第一部分:[SECTIO ...

  7. .net core 灵活读取配置文件

    using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using Syst ...

  8. asp.net core mvc 读取配置文件appsettings.json

    上一篇我们将了读取自定义配置文件.这篇我们讲一下asp.net core mvc里读取自带的配置文件 appsettings.json 首先创建个asp.net core mvc项目,项目里有Prog ...

  9. .net core中读取配置文件

    1)先看丑陋的方法 读取 appsettings.json   然后在 Startup 的 ConfigureServices() 方法中进行注入: public IConfigurationRoot ...

随机推荐

  1. (Java) 模拟http请求调用远程接口

    package com.vcgeek.hephaestus.utils; import java.io.BufferedReader; import java.io.IOException; impo ...

  2. python3 自己写的一个小算法(比对中文文本相似度)

    函数使用说明: 函数的三个参数分别是“匹配语句”,“匹配语料”,“相关度”: 匹配语句,和匹配预料中的语句匹配的语句,必须为字符串: 匹配语料,被匹配语句来匹配的语句列表,必须为列表: 相关度,函数只 ...

  3. 监控tomcat,自动启动

    tomcatID=`ps -ef |grep tomcat |grep -v 'grep'|awk '{print $2}'`  tomcatCount=`ps -ef|grep tomcat |gr ...

  4. mysql 备份 docker mysql备份

    #未用docker安装的 mysqldump -h192.168.1.180 -P3306 -uroot -p123456 demo0201 > bak180814.sql mysql -u用户 ...

  5. 爬虫学习--常用的正则表达式 Day3

    在做爬虫经常遇到需要用正则校验数据时候,往往是在网上去找很久,结果找来的还是不很符合要求.所以我最近把开发中常用的一些正则表达式整理了一下,给自己留个底,也给朋友们做个参考. 一.校验数字的表达式 1 ...

  6. VSCode JAVA环境配置使遇到的几个小问题

    1.出现的问题: The JAVA_HOME environment variable points to a missing or inaccessible folder等三个报错! 2.说明及解决 ...

  7. PHP压缩文件夹的方法

    PHP压缩文件夹的方法<pre> public function addFileToZip($path, $zip) { $handler = opendir($path); //打开当前 ...

  8. Phone Code

    Polycarpus has n friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they ar ...

  9. git回退之git reset

    参考 https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E7%BD%AE%E6%8F%AD%E5%AF%86 https: ...

  10. (C#)WPF:LinearGradientBrush 线性渐变画刷和RadialGradientBrush 圆形渐变画刷

    <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/200 ...