06 - .NET Core Console Application - AppSettings

preview_player
Показать описание
Let's add an app settings dot json file to the Sameworks Project.
• We'll right-click on the project
• Add a new item
• Scroll to find JavaScript JSON Configuration File

Because this file is not part of the build and doesn't get compiled into the code we have to tell the build process to copy it into the same directory where the compiled executables will exist. We'll do so by:
• Select properties
• Change the [Copy to Output Directory] to [Copy Always] or [Copy if newer]
○ It's your choice
○ I prefer [Copy Always]

We'll clear out the contents of the file and replace the body with:
• ConnectionStrings - as you can tell it's a JSON Format
• In ConnectionStrings we'll add a "DefaultConnection"
○ We'll include a value that resembles a valid connection string

• Context
• Configuration

• Optional = true: in case the file doesn't exist
• Reload on Change = true: allows you modify the file while the app is running and reloads the settings automatically.

We'll log it out to the console using a little string interpolation.

Let's run it and see if it works.

In the next video we'll talk about Command Line Arguments… We'll see you there.
Рекомендации по теме
Комментарии
Автор

Thanks this solved the reason why my console app wasnt reading from the appsettings.json file until updated the properties of the file.

gagwithgaffer
Автор

I did this (almost this way) but it seemed so foolish, I couldn't believe I did it correctly. Thanks for confirming. VS can be silly sometimes.

jannarkiewicz
Автор

This helped me a lot, thank you so much!!!

ramiropaz