AppSettings.json example in .NET and C#

AppSettings.json is used in .NET and .NET Core projects for configuration.

Our appsettings.json course focuses on how to get a value, and some of the areas where it can be used in an ASP.NET Core C# app. These include:

  • Read a value from appsettings.json using IConfiguration
  • Use the ConnectionString to integrate with Entity Framework and SQL Server
  • Create an appsettings.json file, specific to a production environment, as well as using IWebHostEnvironment to read the environment name.
  • Implement logging with ILogger

These examples will allow you to view the samples that are used within the course.

Software

This is the software that will need to be installed onto your machine.

  • Visual Studio 2022 17.3.0 above. It will work with the free community version.
  • .NET 6.0 SDK (6.0.401 or above). Visual Studio 2022 should already have the .NET SDK installed.
  • SQL Server 2017 (or above), if you want to use the code sample in part 2

Get the application working

These are the steps to get the application working.

  • Fill out the code example form. We will send you an email where you can download the code sample.
  • For part 2, you'll have to import the SQL database to your local SQL Server. In-addition, you'll have to set the connection string in appsettings.json. An export of the database is located in RoundTheCode.AppSettings .02ConnectionString/Database of the repo. You can use either the .bak or .bacpac file to import it.

You are now ready to go.

Open the project in Visual Studio

There are four parts to the appsettings.json course, and each part has a different solution to view the code.

  • Part 1 - Reading a value and using IConfiguration in ASP.NET Core, open RoundTheCode.AppSettings .01ReadValue.sln
  • Part 2 - Using the appsettings.json ConnectionString with SQL Server and Entity Framework, open RoundTheCode.AppSettings .02ConnectionString.sln
  • Part 3 - Creating an appsettings.production.json file to use in an ASP.NET Core production environment, open RoundTheCode.AppSettings .03Environment.sln
  • Part 4 - Setting up logging to use with ILogger, open RoundTheCode.AppSettings .04Logging.sln

Inside any of the solutions, start the project in Visual Studio, and they will run at https://localhost:9902.