hi,你好!欢迎访问本站!登录
本站由简数采集腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - 文章 - 后端开发 - 正文 看Cosplay古风插画小姐姐,合集图集打包下载:炫龙网 · 炫龙图库

【后端开发】.Net Core怎样读取Json配置文件

2019-11-29后端开发ki4网37°c
A+ A-
媒介:在与传统的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号"
        }
      ]
    }
  }
  //==============================
}

然和再竖立一个与这个节点"雷同"的类

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": "洪山区"
      },
      "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中的内容
            return new ConfigurationBuilder().AddJsonFile("read_json.json")
                                             .Build();

        }

    }
}

在控制器中挪用:

[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
            });
        }

本文来自 C#.Net教程 栏目,迎接进修!

以上就是.Net Core怎样读取Json设置文件的细致内容,更多请关注ki4网别的相干文章!

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  选择分享方式
  移步手机端
【后端开发】.Net Core怎样读取Json配置文件

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签:
推荐阅读

发表评论

选填

必填

必填

选填

请拖动滑块解锁
>>