NetCore根据环境变量加载不同的配置文件
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((context, builder) => { string env = getENV(args); var c = builder.AddJsonFile(path: $"appsettings.{env}.json", optional: true, reloadOnChange: true).Build(); var test = c.GetSection("NacosConfig").GetValue<string>("Namespace"); builder.AddNacosV2Configuration(c.GetSection("NacosConfig")); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); private static string getENV(string[] args) { string env = string.Empty; var ev = Environment.GetEnvironmentVariables(); foreach (var item in ev.Keys) { if (item.ToString().ToLower() == "env") { env = ev[item].ToString(); } } Console.WriteLine($"env_v={env}"); return env; } }
参考:
https://blog.csdn.net/qq_27843785/article/details/104421147
作者:沐雪
文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者和博客园共有,如需转载恳请注明。
如果您觉得阅读这篇博客让你有所收获,请点击右下方【推荐】
为之网-热爱软件编程 http://www.weizhi.cc/