升级后,似乎所有我的.net核心控制台应用程序现在在调试时都会得到错误的路径。

我有以下代码

private static void ConfigureServices(IServiceCollection serviceCollection)
{
        // build configuration
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", false)
            .Build();
}


哪个工作正常,但现在我升级了,但出现以下错误

System.IO.FileNotFoundException: 'The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'D:\projects\myproject\bin\Debug\netcoreapp2.0\appsettings.json'.'

我知道我可以将appsettings.json复制到此路径,但是我以前从未这样做,并且在升级之前还算不错。

最佳答案

我建议完全删除.SetBasePath(Directory.GetCurrentDirectory())行。 SetBasePath方法现在是可选的,如果未调用,则为then we infer the base path from application base

关于您的错误。在dotnet中,There was a fix会改变行为:


  将默认工作目录更改为输出文件夹,而不是项目根目录。


看起来像VS更新一样,您已经安装了.NET Core SDK的最新版本。因此,您需要立即将appsettings.json复制到输出文件夹。

08-05 02:44
查看更多