.NetCore读取Json配置文件的实现示例
.NetCore读取Json配置⽂件的实现⽰例
前⾔:在与传统的asp MVC项⽬相⽐, core项⽬在项⽬⽬录的⽂件结构上和功能上与前者都有很⼤的区别。例如:在 core中使⽤Startup.cs取代Global.asax⽂件⽤于加载应⽤程序的配置和各种启动项。appsettings.json取代fig⽂件⽤于存储应⽤程序所需的配置参数等等。。。
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<string> 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": "娃哈哈⽜奶"
photoshop抠图教程
}
}
],
//------------------------
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
⽐如我们想读取test1
string commodity_test1 = _configuration["Content:0:Trade_name:test1"];
第⼆种:使⽤IOptions<T>来读取json配置⽂件
先把NuGet包导进项⽬:Microsoft.Extensions.Options.ConfigurationExtensions
⾸先在appsettings.json中添加节点如下
{
"Connection": {
"dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456" },奥运会的宗旨
//------------------------
"Content": [
{
"Trade_name": {
"test1": "⼩熊饼⼲",
a股和b股的区别"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<Detailed_address> 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<Information>(Configuration.GetSection("Information"));  #endregion
控制器中使⽤:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace Read.json.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ReadController : Controller
{
private IConfiguration _configuration;
readonly Information _Information;
readonly IOptions<Information> _options;
public ReadController(IConfiguration configuration,
Information Information,
IOptions<Information> options)
{
_configuration = configuration;
_Information = Information;
_options = options;
}
奔驰和宝马[HttpGet]
public async Task<IActionResult> ReadInformation()
{
string Address = _options.Value.school.Region.Province + "-" +
_options.Value.school.Region.City + "-" +
_options.Value.school.Region.Area + "-" +
_options.Value.school.Detailed_address[0].Address + "-" +
_options.Value.school.Introduce.Name + "-" +
_options.Value.school.Introduce.Class + "-" +
_options.Value.school.Introduce.Number;
return Json(Address);
}
[HttpPost]
public async Task<string> ReadJson()
{
string conn = _configuration["Connection:dbContent"];
string commodity = _configuration["Content:0:Trade_name:test1"];  return "";
}
}
}
第三种:这种应该⽐较常见,任意读取⾃定义的json⽂件⾸先建⽴⼀个json⽂件
{
"system_version": {
"Edition": ".Net Core 3.0",
"Project_Name": "Read.json"
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。