前言:在與傳統的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<string> ReadJson() { //读参 string conn = _configuration["Connection:dbContent"]; return ""; } } }</string>
當然也可以讀取數組形式的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
{ "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号" } ] } } //============================== }# 在Startup中加入如下程式碼
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; } } }</detailed_address>控制器中使用:
#region 服务注册,在控制器中通过注入的形式使用 services.AddOptions(); services.Configure<information>(Configuration.GetSection("Information")); #endregion</information>
#第三種:這種應該比較常見,任意讀取自訂的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号" } ] } } //============================== }#再建立一個類別,封裝一個方法
{ "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(); } } }#
## 本文來自
C#.Net教學以上是.Net Core如何讀取Json設定檔的詳細內容。更多資訊請關注PHP中文網其他相關文章!