当前位置:网站首页>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
边栏推荐
- SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
- 杂文 谈谈古典的《拆掉思维里的墙》
- VsCode-Go
- Getting started with JDBC
- 信息摘要、数字签名、数字证书、对称加密与非对称加密详解
- feign报400处理
- Camtasia2022软件新增功能介绍
- Aiot industrial technology panoramic structure - Digital Architecture Design (8)
- 拷贝构造函数 浅拷贝与深拷贝
- MySQL restores data through binlog file
猜你喜欢

Getting started with JDBC

Path environment variable

Milvus 2.0 détails du système d'assurance de la qualité

Shell脚本——Shell编程规范及变量

Use between nodejs modules
![Knowledge points and examples of [seven input / output systems]](/img/5f/759a73836f79ef848f449930dcc1b1.png)
Knowledge points and examples of [seven input / output systems]

自定义my_strcpy与库strcpy【模拟实现字符串相关函数】

Detailed explanation of Niuke - Gloves

vscode如何比较两个文件的异同

面试百分百问到的进程,你究竟了解多少
随机推荐
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
◰GL-阴影贴图核心步骤
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
Do you really understand the principle of code scanning login?
Go language RPC communication
Your brain expands and shrinks over time — these charts show how
Solution architect's small bag - 5 types of architecture diagrams
面试百分百问到的进程,你究竟了解多少
vscode如何比较两个文件的异同
∑GL-透视投影矩阵的推导
Freecodecamp ---- budget & category exercise
Nacos + aspnetcore + Ocelot actual combat code
1-3 nodejs installation list configuration and project environment
聊一聊浏览器缓存控制
MySQL modify master database
Generate random numbers with high quality and Gaussian distribution
El date picker limits the selection range from the current time to two months ago
◰ GL shader handler encapsulation
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
[problem solving] [show2012] random tree