当前位置:网站首页>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
边栏推荐
- oracle 中快速获取表的列名列表
- VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
- ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
- The new MySQL table has a self increasing ID of 20 bits. The reason is
- Sub database and sub table & shardingsphere
- Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
- Promise (III)
- An essay on the classical "tear down the wall in thinking"
- freeCodeCamp----prob_ Calculator exercise
- Detailed explanation of the penetration of network security in the shooting range
猜你喜欢
Nodejs installation and environment configuration
详解牛客----手套
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
Detailed explanation of Milvus 2.0 quality assurance system
RTKLIB 2.4.3源码笔记
Idea of batch manufacturing test data, with source code
ByteVCharts可视化图表库,你想要的我都有
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
Zhongang Mining: Fluorite Flotation Process
面试百分百问到的进程,你究竟了解多少
随机推荐
The new MySQL table has a self increasing ID of 20 bits. The reason is
feign报400处理
Redis docker installation
oracle 中快速获取表的列名列表
RTKLIB 2.4.3源码笔记
How does flash cache data in memory?
PostgreSQL column storage and row storage
Decimal format decimal / datetime conversion processing
MySQL modify master database
Paging SQL
◰GL-阴影贴图核心步骤
批量制造测试数据的思路,附源码
Go language, array, string, slice
Nifi fast installation and file synchronization
El cascade and El select click elsewhere to make the drop-down box disappear
【WPF绑定3】 ListView基础绑定和数据模板绑定
Shell script -- shell programming specification and variables
Smart doc + Torna generate interface document
[registration] tf54: engineer growth map and excellent R & D organization building
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN