.Net Core如何讀取Json配置文件中的參數(shù)?針對這個問題,這篇文章給出了相對應的分析和解答,希望能幫助更多想解決這個問題的朋友找到更加簡單易行的辦法。

.NET Core 是.NET Framework的新一代版本,是微軟開發(fā)的第一個官方版本,具有跨平臺 (Windows、Mac OSX、Linux) 能力的應用程序開發(fā)框架 (Application Framework),未來也將會支持 FreeBSD 與 Alpine 平臺,也是微軟在一開始發(fā)展時就開源的軟件平臺,它經(jīng)常也會拿來和現(xiàn)有的開源 .NET 平臺 Mono 比較。
第一種:使用IOptions<T>來讀取json配置文件
先把NuGet包導進項目:Microsoft.Extensions.Options.ConfigurationExtensions

首先在appsettings.json中添加節(jié)點如下
{
"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": "洪山區(qū)"
},
"Detailed_address": [
{
"Address": "佳園路207號"
}
]
}
}
//==============================
}然和再建立一個與這個節(jié)點"相同"的類
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
控制器中使用:
{
"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": "洪山區(qū)"
},
"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中的內(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"] + "的版本" + "," +
"項目名稱是" + configuration["system_version:Project_Name"];
return Json(new
{
data = system
});
}

第三種:使用IConfiguration接口
我們先在appsettings.json中配置好數(shù)據(jù)庫連接字符串,然后讀取它
{
"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 "";
}
}
}
當然也可以讀取數(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"];
關于.Net Core如何讀取Json配置文件中的參數(shù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享題目:.NetCore如何讀取Json配置文件中的參數(shù)-創(chuàng)新互聯(lián)
分享地址:http://www.chinadenli.net/article4/iddie.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網(wǎng)站設計、移動網(wǎng)站建設、網(wǎng)站營銷、標簽優(yōu)化、網(wǎng)頁設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容