标签:+= code setting length session 文件 variable json ram
public class AppSetting { static IConfiguration Configuration { get; set; } public static bool ReloadOnChange { get; set; } /// <summary> /// 初始化配置文件路径 /// </summary> //static AppSetting() //{ // ReloadOnChange = true; // Configuration = new ConfigurationBuilder(). // Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = ReloadOnChange }).Build(); //} static AppSetting() { //动态根据环境变量不同,获取配置文件 //string path = $"Appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json"; //appsettings.json一定要在输出目录 string path = "Appsettings.json"; //Configuration = new ConfigurationBuilder(). // Add(new JsonConfigurationSource { Path = path, ReloadOnChange = true }).Build(); //直接获取当前启动项的根目录appsetting.json文件。 Console.WriteLine(Directory.GetCurrentDirectory()); Configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()). Add(new JsonConfigurationSource { Path = path, ReloadOnChange = true }).Build(); } public static string app(params string[] sections) { try { var val = string.Empty; for (int i = 0; i < sections.Length; i++) { val += sections[i] + ":"; } return Configuration[val.TrimEnd(‘:‘)]; } catch { return ""; } } public static T GetSession<T>(string sessionName) where T:class { if (string.IsNullOrEmpty(sessionName)) return null; var result = Configuration.GetSection(sessionName).Get<T>(); return result; } }
标签:+= code setting length session 文件 variable json ram
原文地址:https://www.cnblogs.com/luoluoluoD/p/11760532.html