The configuration file 'config.json' was not found and not optional

2.4k views Asked by At

I added a config.json file to my .NetCore 3.1 console app. project. It contains some key-value pairs, connection string etcetc.

Now When I am trying to run my app the exception being thrown in my Program.cs file is as:

The configuration file 'config.json' was not found and is not optional. The physical path is ......

I modified my .csproj file as a hopeful remedy and added following section:

<ItemGroup>
      <Content Update="config.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
   </ItemGroup>

The above didn't help much. Getting the same error nevertheless. I am posting a portion of code stub from Program.cs:

static void Main(string[] args)
        {
            var jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "config.json");

            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(jsonPath);

            var configuration = builder.Build(); <-- here exception
            var connectionString = configuration["connectionString"];
            .....
            .....
            .....
            .....

Any idea what it is? The json file has to be copied to its debug bin folder? I have noticed recently that I am getting a whole bunch of path read/write troubles in my .Net apps. with Visual Studio, even though the paths are just fine.

Some suggestions/advice on this please.

0

There are 0 answers