如何在.NetCore中读取Json配置文件-创新互联
如何在.Net Core中读取Json配置文件?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联是专业的眉县网站建设公司,眉县接单;提供网站设计制作、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行眉县网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!第一种:使用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 TaskReadJson() { //读参 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
先把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 Listdata { 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
控制器中使用:
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_options; public ReadController(IConfiguration configuration, Information Information, IOptions options) { _configuration = configuration; _Information = Information; _options = options; } [HttpGet] public async Task 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 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" } }
再建一个类,封装一个方法
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 TaskReadSystemVersion() { var configuration = _json_File.Read_Json_File(); string system = "使用的是" + configuration["system_version:Edition"] + "的版本" + "," + "项目名称是" + configuration["system_version:Project_Name"]; return Json(new { data = system }); }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。
当前文章:如何在.NetCore中读取Json配置文件-创新互联
网页链接:http://ybzwz.com/article/cdpsic.html