How to read AppSettings values from a .json file in ASP.NET Core?
How to read AppSettings values from a .json file in ASP.NET Core?
308
06-Jul-2023
Updated on 07-Jul-2023
Aryan Kumar
07-Jul-2023Sure. Here is how to read appsettings values from a .json file in ASP.NET Core:
JSON
C#
Here is a breakdown of the code:
UseAppSettingsFile()method adds the appsettings file to the configuration.app.Configuration["MySetting"]property gets the value of the appsetting named "MySetting".Console.WriteLine()method prints the appsetting value to the console.You can also use the
ConfigurationManagerclass to read appsettings values from a .json file. The following code is equivalent to the code above:C#
However, the
ConfigurationManagerclass is deprecated in ASP.NET Core 3.0 and later. You should use theapp.Configurationproperty instead.If you are using a .NET Core 2.1 or earlier version, you can use the
ConfigurationManagerclass to read appsettings values from a .json file. However, it is recommended to upgrade to a newer version of .NET Core and use theapp.Configurationproperty instead.