1、在vscode内输入快捷键 ctrl + shift + P
选择 “NuGet Packge Manager:add package”
安装包:
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration
2、创建 appsettings.json 文件
3、Program.cs改写成这样
using System;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace project
{
class Program
{
static void Main(string[] args)
{
IConfigurationBuilder builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
IConfigurationRoot configurationRoot = builder.Build();
String setting = configurationRoot.GetSection("setting").Value;
Console.WriteLine(setting);
}
}
}
后面自行封装就好了
---------------------------------------重点,重点来了-------------------------------------
build时.netcore不会帮你生成 json配置文件(如果有办法请指正),必须自行把 appsettings.json 复制到生成项目的根目录上