当前位置:网站首页>ASP.NET CORE Study02
ASP.NET CORE Study02
2022-06-28 12:19:00 【人类群星闪耀时】
asp.net core 配置文件加载优先级
默认情况下,项目下 的 launchSettings.json 配置文件的优先级最高,appsettings.Development.json 优先级次之,appsettings.json 配置文件优先级最后。
注意的是,在appsettings.json 下可以更具需求建立多个settings.json ,如development.json ,productionsetting.json 等json 配置文件,每个不同json 文件可以进行专门不同的配置信息,不仅可以使针对开发环境进行独立配置,在较为复杂的业务场景下还可以专门将一部分配置抽离出来,比如connectionsetting.json 专门进行各类连接的配置。
因为launchsettings 配置文件是项目启动是加载的配置,是一定会最先被加载,优先级固然最高,而appsettings 配置信息则是项目运行是加载的信息,优先级当然低于launchsetting配置文件。
asp.net core 读取自定义json 配置信息
方式一:
使用IConfiguration 接口的方式。
- 建立并书写好自定义json 配置文件。
- 在program 类中使用 ConfigureAppConfiguration 方法以指定应用的配置提供程序,即 将自定义的配置json 文件添加到应用程序的配置文件集合中。

- controller 中调用,使用 构造函数注入,由asp.net core 内置容器注入一个IConfiguration 接口类型的对象。

这样就能获取到自定义json 配置文件的信息。
方式二
使用IOptions 接口的方式
- 建立并书写好自定义json 配置文件。
- 在program程序启动类中将json配置文件加入到asp.net core 程序的配置文件集合中,与方式一一致。
- 新建实体类,类名需要和节点名称一致,即 在自定义json 配置文件中 需要读取的具体节点信息的节点名称 保持一致。类型的属性 需要需要读取节点的节点信息的数据类型保持一致,其中节点下的key 值就应是实体类的属性名。
- 在startup 中的ConfigureService 注册服务。

- 在controller 中调用,也是使用构造函数注入,注入一个 定义的实体类,从注入的实体类中获取到节点数据。

这样也可以获取到json 的配置信息。
asp.net core 读取appsettings.json 配置信息
方式一:
使用IConfiguration 接口的方式
- appsettings.json 配置文件已经定义好所需的节点信息,当然可以不一定是appsettings.json 文件也可以是 appsettings.Production.json 或 appsettings.Development.json 文件。例如 读取数据库连接字符串。
- 首先在statup 中的configureService 方法中进行注入,
使用 services.AddSingleton(COnfiguration); 注册。
- 然后在 controller 中调用, 使用构造函数注入,一个 IConfiguration 接口类型的对象,使用使用 .GetSection(“ConnectionStrings”)[“DeefaultConnection”] 类似的方式获取所需的节点信息。
方式二:
使用IOptions 接口的方式
- 书写好对应 appsettings.json 配置文件。
- 定义一个实体类,类名需要和节点的名称保持一致,实体类的属性也推荐和json 的节点名称保持一致,数据类型需要保持一致。
- 在startup 的 configureservice 方法中注册,使用
services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
// todo
这里的注册方式 和 上面的读取自定义json 配置文件中的方式二 的
ConnectionStrings option = new ConnectionStrings();
Configuration.GetSection("ConnectionStrings").Bind(option);
services.AddSingleton(option);
功能是一致的。
- 在controller 中调用,通过构造函数注入,注入一个 IOptions类型的对象,在IOptions 的T 中传入 定义的实体类。使用就是通过对象访问属性的方式。
边栏推荐
- UGUI使用小技巧(五) Scroll Rect组件的使用
- 洛谷_P1303 A*B Problem_高精度计算
- 杰理之wif 干扰蓝牙【篇】
- Unity Editor Extension Foundation, editorguilayout (II)
- AcWing 610. Salary and bonus (implemented in C language)
- 攻防世界新手入门hello_pwn
- JNI confusion of Android Application Security
- Difference (one dimension)
- Url追加参数方法,考虑#、?、$的情况
- 从SimpleKV到Redis
猜你喜欢

RemoteViews布局和类型限制源码分析

In less than an hour, apple destroyed 15 startups

【Unity编辑器扩展基础】、GUI

内部振荡器、无源晶振、有源晶振有什么区别?

分页样式 flex设置成在尾部显示(即使页数加长 也不会因为在末尾而换行)

Leetcode 48. 旋转图像(可以,已解决)

不到一小时,苹果摧毁了15家初创公司

多维度监控:智能监控的数据基础

Ugui force refresh of layout components

. Net hybrid development solution 24 webview2's superior advantages over cefsharp
随机推荐
【C语言】判断三角形
零基础C语言(一)
NFT数字藏品系统开发(3D建模经济模型开发案例)
【Unity编辑器扩展基础】、EditorGUILayout(二)
最新汇总!30省份公布2022高考分数线
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
Redis principle - List
EMC RS485接口EMC电路设计方案
关于字符串转换的一些小技巧
Self use demo of basic component integration of fluent
C语言 sprintf函数使用详解
Using MySQL database in the express framework of node
What are the common modes of financial products in 2022?
. Net hybrid development solution 24 webview2's superior advantages over cefsharp
Unity导入资源后还手动修改资源的属性?这段代码可以给你节约很多时间:AssetPostprocessor
Levels – virtual engine scene production "suggestions collection"
RemoteViews布局和类型限制源码分析
杰理之wif 干扰蓝牙【篇】
运维思考 | 你知道CMDB与监控是什么关系吗?
IDEA全局搜索快捷设置