C# Error – The configuration file ‘appsettings.json’ was not found and is not optional

You receive the following error message when running a C# program that uses a Json configuration file.

The configuration file 'appsettings.json' was not found and is not optional

App Settings

In .NET (C# and ASP.NET), we can store application settings in a file called appsettings.json. The file stores configuration items using key-value pairs.

To use an appconfiguration.json file in C#, we need to install the following .NET package.

dotnet add package Microsoft.Extensions.Configuration.Json --version 7.0.0

Solution

To resolve the error message above, open your application’s project file by right-clicking on the application name in Visual Studio and adding the following lines.

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

You can do the same by clicking the appsettings.json file and setting the copy to an Output directory.


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.