麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 擴展 > JSON > 正文

.Net Core讀取Json配置文件的實現示例

2024-09-06 20:18:10
字體:
來源:轉載
供稿:網友

前言:在與傳統的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 "";  } }}

當然也可以讀取數組形式的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<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": "小熊餅干",  "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號"  }  ] } } //==============================}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91麻豆精品国产91久久久更新资源速度超快 | 龙的两根好大拔不出去h | 中文字幕涩涩久久乱小说 | 成人aaaaa片毛片按摩 | 国产一区二区视频在线播放 | 国色天香综合网 | 国产一区二区久久精品 | 免费国产不卡午夜福在线 | 99亚洲伊人久久精品影院红桃 | 在线观看网址av | 嫩草91在线 | 日本在线一区二区 | 成人做爰s片免费看网站 | 麻豆国产网站 | 涩涩激情网 | 羞羞网站在线看 | 精品国产一区二区三区久久久 | 黄色久| 成人免费毛片片v | 成人免费在线视频 | av在线播放地址 | 日本黄色免费播放 | 在线观看视频毛片 | 免费观看的毛片手机视频 | 亚洲精品欧美在线 | 久久久成人精品视频 | 午夜在线观看视频网站 | 黄色大片网站在线观看 | 欧美a在线看 | 爽爽视频免费看 | 欧美精品国产综合久久 | 日本高清视频网站www | 免费一级特黄欧美大片勹久久网 | 粉嫩蜜桃麻豆免费大片 | 国人精品视频在线观看 | 嫩草91在线| 日本高清在线免费 | 欧美一级黄色免费 | 黄色一级片免费观看 | 内地av在线 | 在线日韩亚洲 |