VS 2019中IIS Express调试器的.Net Core MVC appsettings.json文件在哪里

时间:2019-06-10 16:57:19

标签: c# visual-studio .net-core asp.net-core-mvc visual-studio-2019

我需要在appsettings.json文件中编辑连接字符串,以调试.Net Core MVC应用程序。当我使用IIS Express调试器运行应用程序时,我的应用程序将构建到bin\Debug\netcoreapp2.2上。在此目录中,我正在使用需要测试的值来编辑我的appsettings.Development.json配置文件。我知道应用程序正在提取appsettings.json文件的正确变体。但是,我不认为调试器正在查看bin\Debug\netcoreapp2.2中的文件,因为当我编辑该文件时,更改未出现在我的应用程序中。 IIS Express调试器从哪里加载appsettings.json文件?

更多上下文的屏幕截图。

我从此工具栏运行调试器。

enter image description here

调试器将文件构建到bin\Debug\netcoreapp2.2

enter image description here

然后我编辑必要的appsettings.json文件。由于我将“复制到输出目录”属性设置为“如果较新则复制”,因此该文件在以后的版本中不会被覆盖

enter image description here

我验证了调试器的ASPNETCORE_ENVIRONMENT变量已设置为“ Development”。

enter image description here

但是当我调试我的应用程序时,我在项目的appsettings.json中获得了默认的连接字符串,而在bin\Debug\netcoreapp2.2目录的appsettings.json中没有获得修改后的连接字符串

enter image description here

1 个答案:

答案 0 :(得分:1)

默认情况下,IConfiguration读取项目文件夹下的*.json文件。

要在*.json等其他位置读取bin/Debug/netcoreapp2.2文件,可以像

那样配置ConfigureAppConfiguration
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile(
                    "bin/Debug/netcoreapp2.2/appsettings.Development.json", optional: false, reloadOnChange: true);
            });

然后像

一样使用它
public class HomeController : Controller
{
    private readonly IConfiguration configuration;
    public HomeController(IConfiguration configuration)
    {
        this.configuration = configuration;
    }
    public IActionResult Index()
    {
        return Ok(configuration.GetConnectionString("DefaultConnection"));
        //return View();
    }