Introduction to ASP.NET Core Configuration
This is part 9 of ASP.NET Core tutorial. In this video, we'll discuss the significance of appsettings.json file. In previous versions of ASP.NET, we store application configuration settings like database connection strings, for example, in web.config file. In ASP.NET Core, configuration information can come from different configuration sources. And here are those configuration sources: files, user secrets, environment variables, command line arguments, and even our own custom configuration source. Let's discuss these configuration sources one by one. First, appsettings.json file.
Adding a Key-Value Pair to appsettings.json
This is the same ASP.NET Core project that we've been working with so far in this video series. We generated this project using the empty project template. Notice in the generated project, we already have a file with the name appsettings.json and within this file we already have some default logging settings. We'll discuss these settings in our upcoming videos. For now, let's include a new configuration setting in this appsettings.json file. Configuration information in ASP.NET Core is usually stored as key-value pairs. I'm going to name the key MyKey and the value for this is going to be value of MyKey from appsettings.json.
Injecting the IConfiguration Service
At this point you might be thinking, okay, we have configuration settings stored in this appsettings.json file, but how do I read and access these configuration settings from my application code? Well, for that, ASP.NET Core 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 is files, user secrets, environment variables, and command-line arguments. Now what we want to do is use this IConfiguration service and read our new configuration setting, which has got the key MyKey. We want to be able to access this key value in this startup.cs.
The first thing that we're going to do here is inject the IConfiguration service provided by the framework into this Startup class. For that we need a constructor. To get the constructor, type ctor and then press tab twice. I'm going to use constructor injection to inject IConfiguration service into this Startup class. Notice under IConfiguration we have a red squiggly. While the cursor is on this red squiggly, when I press Control+Period simultaneously, we see the namespace in which this IConfiguration service is present. It's present in Microsoft.Extensions.Configuration namespace. At this point when I hit the Enter key, notice the namespace is automatically included right here. Now let's name this parameter config. To store this injected service, let's create a private field. I'm going to name it _config. Notice we have a red squiggly on our private field _config. This is because we don't have this private field defined yet. At this point when I press Control+Period, notice Visual Studio is providing me with several options. The first option here is to generate this private field. At this point when I press the Enter key, the private field will be automatically generated. There we go.
Reading Configuration Values in Code
Notice we're using dependency injection to inject this IConfiguration service. In previous versions of ASP.NET, dependency injection was optional, and to configure it we have to use external frameworks such as Ninject, StructureMap, etc. In ASP.NET Core, dependency injection is an integral part. Dependency injection allows us to create systems that are loosely coupled, extensible, and easily testable. If you're new to this concept of dependency injection, please do not worry; we'll discuss it in detail in our upcoming videos.
Now what we want to do is read our new configuration setting value that is present in this appsettings.json file. Remember, the name of the key is MyKey. To read that in the Startup.cs file, we're going to use this private field, _config. So instead of writing the current processing that's executing our application, let's write the configuration key value. For that, we're going to use that private field, _config, and remember the name of the key is MyKey. Let's save all our changes and run our project. There we go, we see the configuration setting value as expected.
Using Environment-Specific appsettings Files
In addition to appsettings.json file, we may also have environment-specific appsettings files. For example, for the development environment, we may have appsettings.Development.json. Similarly, 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. We'll discuss these environments in detail in our upcoming videos. For now, just understand in addition to appsettings.json file, we may also have an environment-specific appsettings file.
If we take a look at the Solution Explorer, notice in addition to appsettings.json we also have appsettings.Development.json. The settings in this development environment file are going to override the settings that we have in appsettings.json. Let's actually prove this. Let's make a copy of this setting in our appsettings.Development.json and let's change the value of the key to value of MyKey from appsettings.Development.json. At this point, let's save our changes and reload the webpage. Now the webpage should display the value that is present in appsettings.Development.json. Now, if we comment this and save our changes and reload the webpage, now we should be getting the value from the appsettings file. There we go. So the point that I'm trying to make here, if we have an environment-specific appsettings file, the settings in that file will override the settings in the general appsettings.json file.
The Configuration Override Hierarchy
In addition to files, configuration information in ASP.NET Core can also come from user secrets. We'll discuss user secrets in our upcoming videos. It can also come from environment variables. On our local development machine, we may store environment variables in this launchSettings.json file. Notice we already have an environment variable ASPNETCORE_ENVIRONMENT set to Development. So in addition to this environment variable, let's include another environment variable with the same key as this. So I'm going to copy this and paste it within our launch settings and then change the value here to value of MyKey from environment variable.
Now the important point to keep in mind is this IConfiguration service provided by the framework will read the configuration information that's present in these different configuration sources in the order that is specified right here. Meaning, if we have a setting with a key in appsettings.json and if we have that same setting with that same key in this appsettings.environment.json file also, then the setting in this file will override the setting in appsettings.json. Similarly, if we have the same setting in user secrets, it's going to override the setting in appsettings.environment.json. Similarly, environment variables will override user secrets, and command-line arguments will override environment variables. So the point that I'm trying to get across is later configuration sources override the settings that are present in the earlier configuration sources. Let's prove this now. Let's also uncomment the setting that we have in appsettings.Development.json. At the moment, we have a setting with the same key in these three configuration sources, that is appsettings.json, appsettings.Development.json, and environment variables. If later configuration sources override earlier configuration sources, now when we save these changes that we have here and when we reload our browser page, we should see the setting from the environment variable. So let's reload the web page. There we go. If you don't see the value from the environment variable on your machine, make sure to build your project once and then reload the web page.
Overriding with Command-Line Arguments
If we pass a value for the same key from the command line, then it should override all the previous configuration sources. Let's prove this. To be able to pass command line arguments, we have to run our project from the command line. Here's the command: dotnet run and then the name of the key for which we want to pass a value. In our case, it is MyKey. Finally, the value for it. There we go, we have our application up and running at localhost:5000. So let's launch a new browser tab and navigate to localhost:5000. Notice we see the value that is passed from the command line. This value has overridden all the previous configuration sources.
How Default Configuration is Loaded
In Program.cs file, we have our main method which is the entry point into this application. This method calls CreateWebHostBuilder which in turn calls CreateDefaultBuilder. This is the method that sets up the default order in which all these different configuration sources are read. ASP.NET Core is open source, so we can find the code of this WebHost class on the GitHub page at this URL. I'll have this link available in the description of this video. As of this recording the latest version is 2.2 and that's what is selected. And on this web page, search for ConfigureAppConfiguration and within this method, the default order in which all these different configuration sources are read is setup. Notice first the settings in appsettings.json file are read, followed by that appsettings.environment.json, and then AddUserSecrets, environment variables, and finally command-line arguments. You can change this default order if you want to or even add your own custom configuration source in addition to all these existing configuration sources. We will discuss setting up our own custom configuration source in our upcoming videos. That's it in this video. Thanks for watching.