我可以将 appsettings.json 移出应用程序目录吗?

Ian*_*ton 5 asp.net-core-mvc asp.net-core

我想把我的 appsettings.json 文件放在 web 文件夹之外,这样我就可以避免将它存储在源代码管理中。我怎样才能做到这一点?

Ian*_*ton 10

它似乎不喜欢相对路径。但是,这有效...

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            if (!hostingContext.HostingEnvironment.IsDevelopment())
            {
                var parentDir = Directory.GetParent(hostingContext.HostingEnvironment.ContentRootPath);
                var path = string.Concat(parentDir.FullName, "\\config\\appsettings.json");

                config.AddJsonFile(path, optional: false, reloadOnChange: true);
            }
        })
        .UseStartup<Startup>()
        .Build();
Run Code Online (Sandbox Code Playgroud)

因此,它是默认添加的源之上的另一个设置文件。