.Net Core讀取Json配置文件的實(shí)現(xiàn)示例
前言:在與傳統(tǒng)的asp.net MVC項(xiàng)目相比,.net core項(xiàng)目在項(xiàng)目目錄的文件結(jié)構(gòu)上和功能上與前者都有很大的區(qū)別。例如:在.net core中使用Startup.cs取代Global.asax文件用于加載應(yīng)用程序的配置和各種啟動(dòng)項(xiàng)。appsettings.json取代web.config文件用于存儲(chǔ)應(yīng)用程序所需的配置參數(shù)等等。。。
OK!步入正題,下面來(lái)說(shuō)一下如何讀取Json配置文件中的參數(shù)。
第一種:使用IConfiguration接口
我們先在appsettings.json中配置好數(shù)據(jù)庫(kù)連接字符串,然后讀取它
{
"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 "";
}
}
}

當(dāng)然也可以讀取數(shù)組形式的json,一樣的先在appsettings.json中寫好配置參數(shù),如下:
{
"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<T>來(lái)讀取json配置文件
先把NuGet包導(dǎo)進(jìn)項(xiàng)目:Microsoft.Extensions.Options.ConfigurationExtensions

首先在appsettings.json中添加節(jié)點(diǎn)如下
{
"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": "實(shí)驗(yàn)小學(xué)",
"Class": "中班",
"Number": "15人"
},
"Region": {
"Province": "湖北",
"City": "武漢",
"Area": "洪山區(qū)"
},
"Detailed_address": [
{
"Address": "佳園路207號(hào)"
}
]
}
}
//==============================
}
然和再建立一個(gè)與這個(gè)節(jié)點(diǎn)"相同"的類
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 服務(wù)注冊(cè),在控制器中通過(guò)注入的形式使用
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 "";
}
}
}


第三種:這種應(yīng)該比較常見(jiàn),任意讀取自定義的json文件
首先建立一個(gè)json文件
{
"system_version": {
"Edition": ".Net Core 3.0",
"Project_Name": "Read.json"
}
}

再建一個(gè)類,封裝一個(gè)方法
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()
{
//這句代碼會(huì)讀取read_json.json中的內(nèi)容
return new ConfigurationBuilder().AddJsonFile("read_json.json")
.Build();
}
}
}

在控制器中調(diào)用:
[HttpGet]
public async Task<IActionResult> ReadSystemVersion()
{
var configuration = _json_File.Read_Json_File();
string system = "使用的是" + configuration["system_version:Edition"] + "的版本" + "," +
"項(xiàng)目名稱是" + configuration["system_version:Project_Name"];
return Json(new
{
data = system
});
}


Demo地址:https://github.com/Davenever/Read_Json.git
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net c# 調(diào)用百度pai實(shí)現(xiàn)在線翻譯,英文轉(zhuǎn)中文
本文詳細(xì)介紹asp.net c# 調(diào)用百度pai 實(shí)現(xiàn)在線翻譯以及英文轉(zhuǎn)中文實(shí)現(xiàn)代碼,需要了解的朋友可以參考下2012-12-12
Asp.net 2.0 無(wú)刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
簡(jiǎn)單三步實(shí)現(xiàn)圖片無(wú)刷新上傳:注意是上傳,至于上傳時(shí)的驗(yàn)證,比如圖片的尺寸,大小,格式。自行解決。如果我搞定了,也會(huì)貼上來(lái)的。2013-06-06
VsCode插件開(kāi)發(fā)之插件初步通信的方法步驟
這篇文章主要介紹了VsCode插件開(kāi)發(fā)之插件初步通信的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
.NET?Core控制臺(tái)應(yīng)用ConsoleApp讀取appsettings.json配置文件
這篇文章介紹了.NET?Core控制臺(tái)應(yīng)用ConsoleApp讀取appsettings.json配置文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
asp.net 上傳圖片并作處理(生成縮略圖 、在圖片上增加文字水印、在圖片上生成圖片水?。┑膶?shí)例代碼,經(jīng)過(guò)測(cè)試!2013-06-06
ASP.NET MVC+EF在服務(wù)端分頁(yè)使用jqGrid以及jquery Datatables的注意事項(xiàng)
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC+EF在服務(wù)端分頁(yè)使用jqGrid以及jquery Datatables的注意事項(xiàng),感興趣的小伙伴們可以參考一下2016-06-06
asp.net項(xiàng)目開(kāi)發(fā)中用到的小技巧
項(xiàng)目中用到的小技巧2010-03-03
ASP.NET頁(yè)面優(yōu)化 性能提升8倍的方法
今天與大家分享:一種優(yōu)化頁(yè)面執(zhí)行速度的方法。采用這個(gè)方法,可以使用頁(yè)面的執(zhí)行速度獲得【8倍】的提升效果2012-03-03
使用.net core3.0 正式版創(chuàng)建Winform程序的方法(圖文)
這篇文章主要介紹了使用.net core3.0 正式版創(chuàng)建Winform程序的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

