Introduction
In this article, we’ll discuss the appsettings.json file in the ASP.NET Core application. the significance of appsettings.json file in previous versions of ASP.NET. we store application configuration settings like database connection strings for example in Webconfig.xml file. In an ASP.NET Core, configuration information can come from different configuration sources. Please, read our previous article before proceeding to this article where we learn Kestrel Web Server in ASP.NET Core.
Configurations sources in ASP.NET Core
1.Files(appsettings.json, appsettings.{Environment}.json)
2.User Secrets
3.Environment variables
4.command-line arguments
ASP.NET Core appsettings.json file in Detail
So, Another file that we have in our project is appsettings.json file.
All the application settings are contained in this file and any changes to the app appsettings.json file. it will require restarting of the “IIS administration” to take effect.
Project Solution
Let’s open the project solution to the application and take a look at this file.
So, here we have the appsettings.json file in here right now you see we only have logging we have log level and few other details.
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"MyCustomKey": "Value of Mykey from appsettings.json"
"ConnectionString" : "enter connection string "
}
We will be adding more settings here for the connection string. And also if you add new other details like something related to dependency. if there are some security keys or anything you can also add them here.
You can also store them on a server but app settings are generally a place where you have them.
We will be adding more settings here like connection strings and we’ll be accessing these variables inside the startup.cs class File when we use /link/ dependency injection.
How to Read AppSettings.json file in application code
We read and access these configuration settings from our application code for that ASP.NET Core application. it has provided this IConfiguration service by default. this service is set up in such a way it knows how to read configuration information from all these different configuration sources that file user secrets environment variables and command-line arguments. Now we want to use the IConfiguration service and read our new configuration setting which has got the key. we want to be able to access this key value in this file StartUp.cs
Constructor Injection
the first thing that we’re going to do is inject the IConfiguration service provided by the framework into Startup.cs class for that we need a constructor to get the constructor type to use constructor injection to inject IConfiguration service into this startup class.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace FirstCoreWebApplication
{
public class Startup
{
private IConfiguration _config;
// Below we are using Dependency Injection to inject the Configuration object
public Startup(IConfiguration config)
{
_config = config;
}
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
await context.Response.WriteAsync(_config["MyCustomKey"]);
});
}
}
}
Now, we want to read our new configuration setting value that is present in this AppSettings.json file. So remember the name of the key is Mykey to read that in the startup.cs the file we’re going to use this private field underscore config. So, instead of writing the current processing that’s executing our application.
app.Run(async (context) =>
{
await context.Response.WriteAsync(_config["MyCustomKey"]);
});
Let’s run our project there we go we see the configuration setting value is expected in addition to appsettings.jsonfile.
AppSettings.json file for different environments.
we may also have an environment-specific appsettings.json file for example. we may have appsettings.development.json for a production environment we may have appsettings.production.json and for staging appsettings.staging.json. now, the important point to keep in mind is the settings in this environment-specific file will override the settings in appsettings.json.
Thank you for reading this article, I hope you will understand the ASP.NET Core appsettings.json file. We’ll get into more details about this when we’ll learn Middleware Components And Request Pipeline in ASP.NET Core in the next article.
Ꮐreat article, totaⅼly what I wantеd to find.