.net core使用ConfigurationBuilder读取配置文件
电脑版发表于:2022/5/23 16:19
主要就是SetBasePath设置一个你想要获取配置文章所在的项目
private static IConfigurationRoot BuildConfiguration() { var builder = new ConfigurationBuilder() .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../AJ.JBLand.API/")) .AddJsonFile("appsettings.json", optional: true); return builder.Build(); }
然后拿到对象获取即可,比如获取连接字符串
//方法1: string conn = configuration.GetConnectionString("conn_mysql"); //方法2: string conn_mysql = configuration["ConnectionStrings:conn_mysql"];