C# – ConfigurationSection.Get() returns null

When you use ConfigurationSection.Get() to load an object from appsettings.json, it returns null if the section doesn’t exist. Since you’re probably not expecting this to be null, this can lead to problems surfacing in unexpected places, such as getting a NullReferenceException: Note: If you’re using ASP.NET Core, you’ll be referring to the config via builder.Configuration … Read more

C# – How to programmatically update the User Secrets file

User Secrets are stored in secrets.json. This file is specific to your application. Once you know the path of secrets.json, you can load and update it. Here’s an example of how to update secrets.json programmatically: Note: 1) For brevity, this isn’t showing all using statements. 2) This is using Newtonsoft because it’s better than System.Text.Json … Read more

ASP.NET Core – How to turn off startup logging

When you launch an ASP.NET Core web app, you may see the following startup logging messages: These messages come from having a console logger. If you’re using Host.CreateDefaultBuilder(), it adds a console logger provider by default. There are two simple ways to get rid of these logging messages: turn off logging in appsettings.json or remove … Read more

How to add User Secrets in a .NET Core console app

The User Secrets feature in .NET Core is a safe, simple way to override values in appsettings.json. The overridden values only exist in a file sitting in your own dev environment, so you don’t accidently commit them to your source control repository. This feature is enabled in ASP.NET by default, and the framework does most … Read more

ASP.NET – How to add User Secrets

To add User Secrets in an ASP.NET project in Visual Studio do the following steps: For example, let’s say your appsettings.json looks like this: You want to override ApiKey. In secrets.json you would put this: Now when you run your web API, when you go to use the “ApiKey” from the configuration, it’ll use the … Read more

C# – How to read custom configurations from appsettings.json

The appsettings.json file is a convenient way to store and retrieve your application’s configuration. You can add it to any project and then use the Microsoft.Extensions.Configuration library to work with it. Since appsettings.json is just a JSON file, you can add anything to it (as long as it’s valid JSON). Compared with working with the … Read more