前言:在与传统的asp.net MVC项目相比,.net core项目在项目目录的文件结构上和功能上与前者都有很大的区别。例如:在.net core中使用Startup.cs取代Global.asax文件用于加载应用程序的配置和各种启动项。appsettings.json取代web.config文件用于存储应用程序所需的配置参数等等。。。

OK!步入正题,下面来说一下如何读取Json配置文件中的参数。

第一种:使用IConfiguration接口

我们先在appsettings.json中配置好数据库连接字符串,然后读取它

{ "Connection": { "dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*"}

在控制器中注入IConfiguration接口

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Configuration;namespace Read.json.Controllers{ [ApiController] [Route("[controller]")] public class ReadController : Controller { private IConfiguration _configuration; public ReadController(IConfiguration configuration) { _configuration = configuration; } [HttpPost] public async Task ReadJson() { //读参 string conn = _configuration["Connection:dbContent"]; return ""; } }}

当然也可以读取数组形式的json,一样的先在appsettings.json中写好配置参数,如下:

{ "Connection": { "dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456" }, //------------------------ "Content": [ { "Trade_name": { "test1": "小熊饼干", "test2": "旺仔QQ糖", "test3": "娃哈哈牛奶" } } ], //------------------------ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*"}

比如我们想读取test1

string commodity_test1 = _configuration["Content:0:Trade_name:test1"];

第二种:使用IOptions来读取json配置文件

先把NuGet包导进项目:Microsoft.Extensions.Options.ConfigurationExtensions

首先在appsettings.json中添加节点如下

{ "Connection": { "dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456" }, //------------------------ "Content": [ { "Trade_name": { "test1": "小熊饼干", "test2": "旺仔QQ糖", "test3": "娃哈哈牛奶" } } ], //------------------------ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", //============================== "Information": { "school": { "Introduce": { "Name": "实验小学", "Class": "中班", "Number": "15人" }, "Region": { "Province": "湖北", "City": "武汉", "Area": "洪山区" }, "Detailed_address": [ { "Address": "佳园路207号" } ] } } //==============================}

然和再建立一个与这个节点”相同”的类

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace Read.json{ public class Information { public School school { get; set; } } public class School { public Introduce Introduce { get; set; } public Region Region { get; set; } public List data { get; set; } } public class Introduce { public string Name { get; set; } public string Class { get; set; } public string Number { get; set; } } public class Region { public string Province { get; set; } public string City { get; set; } public string Area { get; set; } } public class Detailed_address { public string Address { get; set; } }}

在Startup中添加如下代码

#region 服务注册,在控制器中通过注入的形式使用 services.AddOptions(); services.Configure(Configuration.GetSection("Information")); #endregion

控制器中使用:

{ "Connection": { "dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456" }, //------------------------ "Content": [ { "Trade_name": { "test1": "小熊饼干", "test2": "旺仔QQ糖", "test3": "娃哈哈牛奶" } } ], //------------------------ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", //============================== "Information": { "school": { "Introduce": { "Name": "实验小学", "Class": "中班", "Number": "15人" }, "Region": { "Province": "湖北", "City": "武汉", "Area": "洪山区" }, "Detailed_address": [ { "Address": "佳园路207号" } ] } } //==============================}

第三种:这种应该比较常见,任意读取自定义的json文件

首先建立一个json文件

{ "system_version": { "Edition": ".Net Core 3.0", "Project_Name": "Read.json" }}

再建一个类,封装一个方法

using Microsoft.Extensions.Configuration;using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace Read.json{ public class Json_File { public IConfigurationRoot Read_Json_File() { //这句代码会读取read_json.json中的内容 return new ConfigurationBuilder().AddJsonFile("read_json.json") .Build(); } }}

在控制器中调用:

[HttpGet] public async Task ReadSystemVersion() { var configuration = _json_File.Read_Json_File(); string system = "使用的是" + configuration["system_version:Edition"] + "的版本" + "," + "项目名称是" + configuration["system_version:Project_Name"]; return Json(new { data = system }); }

本文来自 C#.Net教程 栏目,欢迎学习!

netcore读取json文件_.Net Core如何读取Json配置文件相关推荐

  1. netcore读取json文件_.net core读取json格式的配置文件

    在.Net Framework中,配置文件一般采用的是XML格式的,.NET Framework提供了专门的ConfigurationManager来读取配置文件的内容,.net core中推荐使用j ...

  2. python如何打开json文件_如何使用python打开json文件?

    工程_请求数据.json公司名称:{ "appKey": "9c9fa7201e90d3d96718bc3f36ce4cfe1781f2e82f4e5792996623b ...

  3. python按列读取txt文件_如何使用pandas读取txt文件中指定的列(有无标题)

    最近在倒腾一个txt文件,因为文件太大,所以给切割成了好几个小的文件,只有第一个文件有标题,从第二个开始就没有标题了. 我的需求是取出指定的列的数据,踩了些坑给研究出来了. import pandas ...

  4. python如何读取log文件_怎么解决Python读取log文件时报错

    怎么解决Python读取log文件时报错 发布时间:2020-05-23 14:15:56 来源:亿速云 阅读:157 作者:鸽子 问题描述: 写了一个读取log文件的Python脚本:# -*- c ...

  5. python读取sav文件_在Python中读取SPSS(.sav)文件时,获取“title already used as a name or title”错误...

    我正在读一个SPSS文件(.sav).我下面的代码可以读取.sav文件.但是,我遇到了一个非常奇怪的错误.当我试图读取另一个.sav文件时,它会给出以下错误Traceback (most recent ...

  6. import引入json文件_关于TypeScript中import JSON的正确姿势详解

    前言 Typescript是微软内部出品的,用actionscript的语法在写js的一门新语言,最近 TypeScript 中毒,想想我一个弱类型出身的人,怎么就喜欢上了类型约束--当然这不是重点, ...

  7. 本地服务器json文件,从本地ftp服务器读取Json文件

    我是存储在本地服务器中的名为File1.js的json文件.我想读取json文件的内容,并希望在其他文件中显示数据.我已经尝试使用JavaScript编码,但它不能正常工作.从本地ftp服务器读取Js ...

  8. 【Unity3D读取数据】(二)Json文件操作(创建、读取、解析、修改)

    推荐阅读 CSDN主页 GitHub开源地址 Unity3D插件分享 简书地址 我的个人博客 QQ群:1040082875 大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有 ...

  9. flex bison解析json文件_每秒解析千兆字节的 JSON 解析器开源了

    本文转自我们的网站 InfoQ,译者无明.除了推荐 simdjson 之外,还想测试一下微信平台编辑器的代码样式功能. 事实证明,微信文章的代码展示能力很强了.非常棒. 近日,GitHub 开源了一 ...

最新文章

  1. ubuntu流量监控_linux - 实时流量监控
  2. 自己做站点(一) 从头至尾实现一个企业站的速成
  3. HttpContext.Current.Cache在控制台下不工作
  4. MySQL--字符编码和字符集
  5. [Linux] PHP程序员玩转Linux系列-自动备份与SVN
  6. html图片、背景音乐、滚动文字
  7. 转载HTML实体字符
  8. 基于asyncio编写一个telegram爬虫机器人
  9. 嵌入式linux数控系统,关于ARM+DSP嵌入式Linux数控系统设计.pdf
  10. 安卓编程 app图标自定义
  11. 达梦数据库DCA认证培训笔记
  12. 尝试使用OPENMV识别装甲板
  13. 第二天,向导制作LQFP封装及技巧
  14. 【Dubbo】no provider available for the service错误解决方案
  15. Python图片转灰度矩阵-矩阵转图片-jupyter内展示图片#将图片转换为矩阵
  16. 前端js实现正则表情包内容替换
  17. atan2反正切快速近似算法
  18. detectron2的构建安装与常见问题
  19. 面试常问:Mybatis 使用了哪些设计模式?
  20. soap开票服务器系统,使用 SOAP 服务

热门文章

  1. 【童心制物】一篇很硬的标新立异级别的体验测评——聊新版造物编程盒
  2. 稻盛和夫《活法》读后感
  3. 【Verilog】模16可逆流水灯
  4. 基于FPGA的BP神经网络的verilog实现
  5. 009_关闭linux的THP
  6. Java中windows路径转换成linux路径等工具类
  7. 视频营销、B2B营销、EDM营销之营销方式大PK
  8. 关于expanded一级二级菜单数据的分组排序
  9. Team Leader你会带团队吗?你懂合作吗?你好像都不会啊!(上)
  10. early z optimization