关于为什么会抛出“找不到配置文件且不是可选”异常的任何提示?
string relative_path = "../../../../appsettings_global.json";
if (File.Exists(relative_path)) {
IConfigurationRoot global_config = new ConfigurationBuilder().AddJsonFile(relative_path, false, true).Build();
global_settings = global_config.GetRequiredSection("Settings").Get<GLOBAL_SETTINGS>();
}
我发现以下内容可以满足我的需要;即,将相对路径转换为绝对路径。如果有其他/更好的答案,请发布。
string absolute_path = System.IO.Path.GetFullPath("../../../../appsettings_global.json");
IConfigurationRoot global_config = new ConfigurationBuilder().AddJsonFile(absolute_path, false, true).Build();
global_settings = global_config.GetRequiredSection("Settings").Get<GLOBAL_SETTINGS>();