当前位置:网站首页>ASP. Net core reads the configuration file in the class library project
ASP. Net core reads the configuration file in the class library project
2022-04-23 17:04:00 【begeneral】
Go straight to the code :
public static string ReadConnectString(string connectName)
{
var config = new ConfigurationBuilder().Add(new JsonConfigurationSource()
{
Path = "appsettings.json",
Optional = true
}).Build();
var connectionString = config.GetConnectionString(connectName);
if(string.IsNullOrEmpty(connectionString))
{
LogHelper.WriteErrorLog($" stay appsettings.json in , Could not find name {connectName} Connection string of ");
return null;
}
return connectionString;
}
The configuration file is as follows :
{
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database= Database name ;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Follow this configuration file , The parameter passed in by the above function should be :DefaultConnection.
I want to introduce 2 An assembly :
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
What I write here is read only appsettings.json The connection string inside , If you want to read the custom configuration , Call GetSection.
版权声明
本文为[begeneral]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554082264.html
边栏推荐
- MySQL modify master database
- Sub database and sub table & shardingsphere
- PostgreSQL column storage and row storage
- Scope and scope chain in JS
- MySQL restores data through binlog file
- TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘
- 1-5 nodejs commonjs specification
- Kingdee Cloud Star API calling practice
- 1-3 nodejs installation list configuration and project environment
- 批量制造测试数据的思路,附源码
猜你喜欢
Milvus 2.0 質量保障系統詳解
Get the column name list of the table quickly in Oracle
Feign report 400 processing
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
ByteVCharts可视化图表库,你想要的我都有
Shell脚本——Shell编程规范及变量
RTKLIB 2.4.3源码笔记
Nifi fast installation and file synchronization
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
groutine
随机推荐
Further study of data visualization
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
Basic case of Baidu map
Handwritten event publish subscribe framework
VsCode-Go
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
Derivation of Σ GL perspective projection matrix
Introduction to how to set up LAN
Paging the list collection
PyMySQL
Promise (I)
Idea of batch manufacturing test data, with source code
[registration] tf54: engineer growth map and excellent R & D organization building
How does flash cache data in memory?
English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
Your brain expands and shrinks over time — these charts show how
An essay on the classical "tear down the wall in thinking"
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
Redis docker installation
Loggie source code analysis source file module backbone analysis