当前位置:网站首页>ASP. NET CORE Study02
ASP. NET CORE Study02
2022-06-28 12:30:00 【When the human stars shine】
asp.net core Profile load priority
By default , Under the project Of launchSettings.json Configuration files have the highest priority ,appsettings.Development.json Priority level is second ,appsettings.json Profile priority last .
Pay attention to is , stay appsettings.json You can create more than one settings.json , Such as development.json ,productionsetting.json etc. json The configuration file , Each is different json File can be used for different configuration information , Not only can it be configured independently for the development environment , In more complex business scenarios, you can also extract some configurations , such as connectionsetting.json Special configuration for various connections .
because launchsettings The configuration file is the configuration loaded when the project is started , It must be loaded first , Of course, the highest priority , and appsettings The configuration information is the information that the project runs and loads , Priority is certainly lower than launchsetting The configuration file .
asp.net core Read custom json Configuration information
Mode one :
Use IConfiguration How to interface .
- Create and write custom json The configuration file .
- stay program Class ConfigureAppConfiguration Method to specify the configuration provider for the application , namely Will customize the configuration json File is added to the application's configuration file collection .

- controller Call in , Use Constructor injection , from asp.net core The built-in container injects a IConfiguration Object of interface type .

In this way, you can get the customization json Profile information .
Mode two
Use IOptions How to interface
- Create and write custom json The configuration file .
- stay program In the program startup class json Configuration file added to asp.net core In the configuration file collection of the program , One by one with the way .
- New entity class , The class name should be consistent with the node name , namely In the custom json In profile The node name of the specific node information to be read bring into correspondence with . Properties of type The data type of the node information that needs to be read must be consistent , Among them, under the node key The value should be the attribute name of the entity class .
- stay startup Medium ConfigureService Registration service .

- stay controller Call in , Constructor injection is also used , Into a Defined entity class , Get the node data from the injected entity class .

In this way, you can also get json Configuration information .
asp.net core Read appsettings.json Configuration information
Mode one :
Use IConfiguration How to interface
- appsettings.json The configuration file has defined the required node information , Of course not necessarily appsettings.json Documents can also be appsettings.Production.json or appsettings.Development.json file . for example Read database connection string .
- First, in the statup Medium configureService Method ,
Use services.AddSingleton(COnfiguration); register .
- And then in controller Call in , Use constructor Injection , One IConfiguration Object of interface type , use .GetSection(“ConnectionStrings”)[“DeefaultConnection”] Get the required node information in a similar way .
Mode two :
Use IOptions How to interface
- Write the corresponding appsettings.json The configuration file .
- Define an entity class , The class name should be consistent with the name of the node , The attributes of entity classes are also recommended and json The node names of are consistent , Data types need to be consistent .
- stay startup Of configureservice Method registration , Use
services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
// todo
The registration method here and Read custom... Above json Mode 2 in the configuration file Of
ConnectionStrings option = new ConnectionStrings();
Configuration.GetSection("ConnectionStrings").Bind(option);
services.AddSingleton(option);
The function is consistent .
- stay controller Call in , Inject through the constructor , Into a IOptions Object of type , stay IOptions Of T In the middle of Defined entity class . Use is the way to access attributes through objects .
边栏推荐
猜你喜欢

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

ASP.NET CORE Study04

UGUI强制刷新Layout(布局)组件

ASP.NET CORE Study02

深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...

UGUI使用小技巧(六)Unity实现字符串竖行显示

Mathematical principle derivation of structured light phase shift method + multifrequency heterodyne

Tips for using ugui (V) using scroll rect component

ASP.NET CORE Study05

Unity Editor Extension Foundation, editorguilayout (II)
随机推荐
After importing resources, unity also manually modifies the properties of resources? This code can save you a lot of time: assetpostprocessor
js 期约与异步函数 Promise
ASP.NET CORE Study05
[unity Editor Extension practice], find all prefabrications through code
【Unity编辑器扩展实践】、利用txt模板动态生成UI代码
最新!基于Open3D的点云处理入门与实战教程
杰理之wif 干扰蓝牙【篇】
【经验分享】Django开发中常用到的数据库操作总结
杰理之wif 干扰蓝牙【篇】
MapReduce project case 1
【Unity编辑器扩展基础】、EditorGUILayout (一)
Jerry's wif interferes with Bluetooth [chapter]
Jerry's wif interferes with Bluetooth [chapter]
FineReport安装教程
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
What is data compliance? How to achieve data compliance?
Jerry's SPI1 plug-in flash recording modification [part]
Function and principle of remoteviews
Jerry's wif interferes with Bluetooth [chapter]
UGUI强制刷新Layout(布局)组件