当前位置 主页 > 服务器问题 > Linux/apache问题 >

    .Net Core读取Json配置文件的实现示例

    栏目:Linux/apache问题 时间:2019-11-26 15:40

    前言:在与传统的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号"
      }
      ]
     }
     }
     //==============================
    }