当前位置:网站首页>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
边栏推荐
- org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
- How to build tiktok user trust and drive fan growth
- PostgreSQL column storage and row storage
- RTKLIB 2.4.3源码笔记
- 聊一聊浏览器缓存控制
- Introduction to new functions of camtasia2022 software
- STM32__ 03 - beginner timer
- Expression "func" tSource, object "to expression" func "tSource, object" []
- Scope and scope chain in JS
- VsCode-Go
猜你喜欢
Go language, array, string, slice
org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
线性代数感悟之2
Milvus 2.0 質量保障系統詳解
feign报400处理
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
How much do you know about the process of the interview
TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘
STM32__ 03 - beginner timer
Nodejs installation and environment configuration
随机推荐
Camtasia2022软件新增功能介绍
Expression "func" tSource, object "to expression" func "tSource, object" []
Nifi fast installation and file synchronization
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
Some problems encountered in recent programming 2021 / 9 / 8
面试百分百问到的进程,你究竟了解多少
Talk about browser cache control
Knowledge points and examples of [seven input / output systems]
STM32__03—初识定时器
1-3 nodejs installation list configuration and project environment
Lock锁
Smart doc + Torna generate interface document
Linux MySQL data timing dump
El cascade and El select click elsewhere to make the drop-down box disappear
New keyword learning and summary
Idea of batch manufacturing test data, with source code
How vscode compares the similarities and differences between two files
[PROJECT] small hat takeout (8)
Production environment——
PostgreSQL column storage and row storage