/// <summary>
/// 读取配置信息
/// </summary>
public class Zconfig
{
#region 读取配置信息
/// <summary>
/// 读取配置信息
/// </summary>
/// <param name="name">key</param>
/// <returns></returns>
public static string Getconfig(string name)
{
IConfiguration config = new ConfigurationBuilder().Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true }).Build();
var appconfig = new ServiceCollection()
.AddOptions()
.Configure<SiteConfig>(config.GetSection("SiteConfig"))
.BuildServiceProvider()
.GetService<IOptions<SiteConfig>>()
.Value; return appconfig.Configlist.FirstOrDefault(o => o.Name == name).Info;
}
#endregion } #region 读取配置相关类
public class SiteConfig
{ public List<SiteConfiglist> Configlist { get; set; }
}
public class SiteConfiglist
{
public string Name { get; set; }
public string Info { get; set; }
}
#endregion
通过在appsettings.json配置节点,再通过以上方法 传入name的值读取info的值