当前位置:网站首页>C # - how to add and read appsetting in the console application JSON file
C # - how to add and read appsetting in the console application JSON file
2022-06-11 14:24:00 【allway2】
In this article , We'll learn how to do it in .NET Core Add and read from the console application appsettings.json file .
We turn on Visual Studio And create one called “ TestJson ” Console application for (.NET Core) project :

Now? , We add a name called “Parameters.json” The file of , We will store one in it DB The connection string and the two named ValueABC and ValueDEF Parameters of :
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DbConnection": "ExampleTestConnection"
},
"Parameter": {
"ValueABC": "TestValue_ABC",
"ValueDEF": "TestValue_DEF"
}
}
Important note : We must change the properties of the file “ Copy to output directory ” Set to :“ If newer, copy ”:

Now? , In order to read Parameters.json file , We have to use NuGet Package add three libraries :
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json
Last , We modify Program.cs To read appsetting file :
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
namespace TestJson
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting reading file .json");
Console.WriteLine($"ConnectioString: {GetDbConnection()}");
Console.WriteLine($"The parameters are: {GetParameters()}");
Console.ReadLine();
}
private static string GetDbConnection()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("Parameters.json", optional: true, reloadOnChange: true);
string strConnection = builder.Build().GetConnectionString("DbConnection");
return strConnection;
}
private static string GetParameters()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("Parameters.json", optional: true, reloadOnChange: true);
var val1 = builder.Build().GetSection("Parameter").GetSection("ValueABC").Value;
var val2 = builder.Build().GetSection("Parameter").GetSection("ValueDEF").Value;
return $"The values of parameters are: {val1} and {val2}";
}
}
}If we run the application , This will be the result :

边栏推荐
- Leetcode 1963. Minimum number of swaps to balance strings (learning)
- Operating instructions for communication between RS485 (Modbus RTU) industrial RFID reader ck-fr03-a01 and PLC Mitsubishi fx5u
- NoSQL之Redis配置与优化
- In depth research and analysis report on global and Chinese high purity molybdenum market
- JSTL custom label
- Recommended open source scheduling libraries worth learning
- 2021-2027 China scaffold and accessories market status analysis and development prospect forecast report
- 2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案
- In depth research and analysis report on global and Chinese gas monitor market
- MySQL数据库创建索引的方法和好处
猜你喜欢

Ali talked about the use of strategic mode in the project

2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案

The application of machine learning in database cardinality estimation

Webgl programming guide learning (0)

How to quickly compress the size of video?

Airtest automated test

Telecommuting with cpolar (1)

. Net C Foundation (6): namespace - scope with name

MySQL advanced statement

What's a good gift for the goddess Festival on March 8? Goddess Day gift sharing
随机推荐
[team learning] task06:for, if, and while
d区间到可空转换
[acwing 237. program automatic analysis] parallel search + discretization
HR doesn't want to read such a PDF technical resume at all. How can it be in the hands of the interviewer?
提取式存储才是最佳的记忆方法
2022-2028 global and Chinese near field scanning optical microscope (NSOM) market status and future development trend
In depth research and analysis report on global and Chinese p-chlorotrifluoromethane Market
What's a good gift for the goddess Festival on March 8? Goddess Day gift sharing
[public class preview]: mxplayer Ott audio and video transcoding practice and optimization
Implementation of VGA protocol based on FPGA
MySQL advanced statement
My struggle: my years in foreign enterprises (1)
Global and China dynamic light scattering nano laser particle sizer market depth research and Analysis Report
In depth research and analysis report on global and Chinese sanitary safety product market
How to quickly compress the size of video?
基于Qt开发实现的任务管理器
JSTL custom label
How to quickly make the title and ending with one click?
In depth research and analysis report on global and Chinese diet food market
JSTL 自定义标签