Configuration
ASP.NET Core supports many methods of configuration. In ASP.NET Core application, the configuration is stored in name-value pairs and it can be read at runtime from various parts of the application. The name-value pairs may be grouped into multi-level hierarchy. The application configuration data may come from
File, such as JSON, XML, INI
Environment variables
Command
Line arguments
An in-memory collection
Custom providers
Configuration System in ASP.NET Core is restructured from the older version of ASP.NET. The older version uses "System.Configuration" namespace and is able to read XML configuration file such as web.config. The new configuration model can be accessed to the key/value based settings and it can retrieve various sources, such as JSON, XML and INI.
Example of reading configuration using JSON provider
Generally, configuration remains in simple key/value structure or might in hierarchical structure when using external files. Consider the following appsetting.json
file for this example.
The AddJsonFile
method of JsonConfigurationExtensions
class is used to JSON file to builder. We can get simple configuration value by using key name. If configuration value is in hierarchical structure, it can be retrieved using a :
seperated key, starting from root of the hierarchy. In this example, if we want to get value for DefaultConnection
, then the key becomes ConnectionStrings:DefaultConnection
.
Retrieve Configuration Data at controller
In the above mentioned example, I am retrieving the configuration value in startup class itself. The new version of ASP.NET has built-in support for dependency injection. Using DI, we can inject the value of configuration to the Controller.
Using AddSingleton
method of ServiceCollectionServiceExtensions
class, we can add a singleton service of the specified type with an instance of service.
Get Configuration object using options pattern
The Options pattern enables us to use custom option classes to represent a group of related settings. The Option class must have public read-write property that is defined for each setting and the class must not take any parameters. The Microsoft.Extensions.Options.ConfigurationExtensions
dependency contains the extension method for IServiceCollection.Configure
.
Here, I have defined my model and I am using same appsetting.json
that was used in previous example. In this example, I have bound the connection string setting of appsetting.json
with my model class.
In the following code, IConfigureOptions
service is added to the Service Container. It binds ConnectionStrings
class to the section ConnectionStrings
of the appsettings.json
file.
Working with In-memory provider
The new configuration framework of ASP.NET Core does also support in-memory configuration. In this type of configuration, the values are directly stored into the code and the later part of application uses this configuration. Following is a sample that shows how to use the in-memory provider and bind to a class.
Using Configuration["Profile:FirstName"]
, we can get the value of firstname of profile configuration. We can also bind this value with custom model. Her,e I have created the following “Profile” class to bind this profile value.
The following sample shows how to bind to a profile and use the options pattern with ASP.NET Core MVC application.
Last updated