1、定义对应读取类ReadConfig.
2、对应类中函数
private static IConfigurationRoot builder;
static ConfigHelper()
{
builder = new ConfigurationBuilder()
.AddInMemoryCollection()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.Build();
}
public static string GetAppStrubutie(string keyName) =>
builder.GetSection("appsettings.json中属性名称")[keyName];
3、上述需要引用using Microsoft.Extensions.Configuration;
using System.IO;
4、调用函数:
string strName = ReadConfig.GetAppStrubutie("属性名称"); //属性名称