栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

NETCore入门系列(读取appsetting.json配置文件内容)

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NETCore入门系列(读取appsetting.json配置文件内容)

文章目录
    • 一、创建项目
    • 二、配置文件读取方式一
      • start.up中输出配置文件信息
      • 控制器中输出配置文件信息
    • 三、配置文件读取方式二
    • 四、源码

一、创建项目

1、创建一个NETCore5.0的Web API项目。
2、appsetting.json中的内容如下:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "SchoolName": "咸鱼一中",
  "Student": {
    "ClassName": "高一一班",
    "StudentList": [
      "张三",
      "李四",
      "王五"
    ]
  }
}
二、配置文件读取方式一

通过**IConfiguration[参数名称]**方式读取appsetting.json中的配置信息。

start.up中输出配置文件信息

1、通过startup.cs构造函数注入IConfiguration

2、startup.cs中的代码如下:

 public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
Console.WriteLine("SchoolName:"+Configuration["SchoolName"]);
			//寻找某个对象的子对象,则获取方式为"当前对象:子对象"
            Console.WriteLine("ClassName:" + Configuration["Student:ClassName"]);
			//寻找某个数组对象指定下标值得方式为"数组:下标"
            Console.WriteLine("StudentList[0]:" + Configuration["Student:StudentList:0"]);
            Console.WriteLine("StudentList[1]:" + Configuration["Student:StudentList:1"]);
            Console.WriteLine("StudentList[2]:" + Configuration["Student:StudentList:2"]);
        }

//省略Configure()

3、运行项目,可看到控制台中依次输出appsetting.json中想应的内容:

控制器中输出配置文件信息

1、通过startup.cs构造函数注入IConfiguration
2、具体代码如下:

private readonly IConfiguration _configuration;
        public WeatherForecastController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
[HttpGet]
        [Route("GetConfig")]
        public string GetConfig()
        {
            return $"SchoolName:{_configuration["SchoolName"]}n" +
                $"ClassName:{_configuration["Student:ClassName"]}n" +
                $"StudentList[0]:{_configuration["Student:StudentList:0"]}n" +
                $"StudentList[1]:{_configuration["Student:StudentList:1"]}n"+
                $"StudentList[2]:{_configuration["Student:StudentList:2"]}n";
        }

3、浏览器中访问"https://localhost:5001/Weatherforecast/GetConfig",可看到正确输出appsetting.json中对应的配置文件信息。

三、配置文件读取方式二

1、创建一个名为StudentOptions的实体类,代码如下:

public class StudentOptions
    {
        /// 
        /// 班级名
        /// 
        public string ClassName { get; set; }

        public List StudentList { get; set; }
    }

2、startup.cs中的添加代码如下:

public void ConfigureServices(IServiceCollection services)
{
  //省略其他代码
 //将配置文件中的数据映射到实体类
            services.Configure(Configuration.GetSection("Student"));
}

3、控制器中的代码如下:

        private readonly IConfiguration _configuration;
        private readonly StudentOptions _studentOptions;

        public WeatherForecastController(IConfiguration configuration, IOptions options)
        {
            _configuration = configuration;
            _studentOptions = options.Value;
        }
        
		[HttpGet]
        [Route("GetConfigByEntity")]
        public string GetConfigByEntity()
        {
            return JsonConvert.SerializeObject(_studentOptions);
        }

4、浏览器中访问"https://localhost:5001/Weatherforecast/GetConfigByEntity",可看到正确输出appsetting.json中对应的Student配置文件信息

四、源码

https://gitee.com/wusuoweixgy/NET5Code/tree/master/ConfigReadWebApp

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023299.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号