0

I need to edit a connection string in my appsettings.json file for debugging a .Net Core MVC application. When I run the application with the IIS Express debugger, my application is built to bin\Debug\netcoreapp2.2. From within this directory, I'm editing my appsettings.Development.json configuration file with the values I need for testing. I know the application is pulling the right variation of the appsettings.json files. However, I don't think the debugger is looking at the file in bin\Debug\netcoreapp2.2, since when I edit that file the changes are not present in my application. Where does the IIS Express debugger load the appsettings.json files from?

Screenshots for more context.

I run the debugger from this tool bar.

enter image description here

The debugger builds the files to bin\Debug\netcoreapp2.2.

enter image description here

I then edit the necessary appsettings.json file. The file is not overwritten in future builds since I have the "Copy To Output Directory" property set to "Copy if Newer"

enter image description here

I verified that the ASPNETCORE_ENVIRONMENT variable is set to "Development" for the Debugger.

enter image description here

But then when I go to debug my app, I get the default connection string in the project's appsettings.json and not the modified connection string in the bin\Debug\netcoreapp2.2 directory's appsettings.json

enter image description here

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
  • Hmm, that's not ideal. Is there a way to configure it to look at the bin folder instead? – Chris Stillwell Jun 10 '19 at 17:20
  • Maybe this question can help you: https://stackoverflow.com/questions/37858312/dotnet-publish-doesn%C2%B4t-publish-correct-appsettings-env-environmentname-json – Flavio Francisco Jun 10 '19 at 18:33
  • @ChrisStillwell Why is that not ideal to have it in root folder? – AliK Jun 11 '19 at 03:06
  • @AliK, mainly because the file I'm adding the connection string to is under source control and I don't want to risk accidentally pushing credentials to the repository. If I could edit the file in the bin folder I wouldn't need to worry about that. – Chris Stillwell Jun 11 '19 at 14:02
  • I would have appsettings.json which is your main file that always commits to source control and then a copy called appsettings.development.json which is the one that runs when you start your app in visual studio. The .development.json would never be committed to source control and each developer could have their own copy of the .dev.json file. This approach works well. – AliK Jun 12 '19 at 00:47

1 Answers1

1

By default, IConfiguration read the *.json file under the project folder.

For reading *.json file in other places like bin/Debug/netcoreapp2.2, you could configure ConfigureAppConfiguration like

    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);
            });

And then use it like

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();
    }
Edward
  • 28,296
  • 11
  • 76
  • 121