当前位置:网站首页>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
边栏推荐
- Lock锁
- Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
- How vscode compares the similarities and differences between two files
- PHP高效读大文件处理数据
- Zhimeng dedecms security setup Guide
- 聊一聊浏览器缓存控制
- 1-4 configuration executable script of nodejs installation
- Zhongang Mining: Fluorite Flotation Process
- New keyword learning and summary
- Milvus 2.0 détails du système d'assurance de la qualité
猜你喜欢
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
1-4 configuration executable script of nodejs installation
How to build tiktok user trust and drive fan growth
STM32__03—初识定时器
[PROJECT] small hat takeout (8)
Use between nodejs modules
Further study of data visualization
Path environment variable
Node access to Alipay open platform sandbox to achieve payment function
RTKLIB 2.4.3源码笔记
随机推荐
1-5 nodejs commonjs specification
Paging SQL
PHP高效读大文件处理数据
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
Nifi fast installation and file synchronization
PostgreSQL列存与行存
Quick install mongodb
Milvus 2.0 質量保障系統詳解
About stream flow, write it down briefly------
Bottom processing of stack memory in browser
Calculate pie chart percentage
freeCodeCamp----shape_ Calculator exercise
Expression "func" tSource, object "to expression" func "tSource, object" []
织梦DEDECMS安全设置指南
VsCode-Go
[PROJECT] small hat takeout (8)
线性代数感悟之2
批量制造测试数据的思路,附源码
Promise (II)