appsettings.json in .NET Core
In ASP.NET Core, appsettings.json is a configuration file that can be used to store various application settings, such as database connection strings, service endpoints, and other application-specific settings.
The appsettings.json file is typically located in the root folder of the ASP.NET Core project and is used to store settings that can be accessed from the application using the Microsoft.Extensions.Configuration namespace. The configuration system in ASP.NET Core allows you to define your application settings in various ways, such as in JSON files, environment variables, and command-line arguments, and then access them using a unified API.
Here’s an example of an appsettings.json file that defines a connection string and a few other application settings:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDb;Trusted_Connection=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*"
}To access the settings in the appsettings.json file, you can use the Configuration object in the Startup class. For example:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }You can then access the settings using the GetValue method of the Configuration object. For example:
string connectionString = Configuration.GetValue<string>("ConnectionStrings:DefaultConnection");You can also use the appsettings.json file to store different configuration values for different environments, such as development, staging, and production. To do this, you can use the appsettings.{Environment}.json naming convention, where {Environment} is the name of the environment. For example, you might have separate appsettings.Development.json, appsettings.Staging.json, and appsettings.Production.json files for different environments. The ASP.NET Core runtime will automatically use the appropriate file based on the current environment.
Thanks for reading! Hope you found it useful. Want more? Hit the “Follow” button below my profile. With your support, I’ll keep creating awesome content for you. Have a great day ahead! — Fuji Nguyen





