ASP.NET 5 Configuration – Custom Providers

We’ve used the configuration providers that come with ASP.NET 5, and now we’ll explore creating custom providers for those times when our settings come from a source the built-in providers don’t handle.

Creating a custom configuration provider is remarkably easy, aside from the work to retrieve the configuration data. All we need to do is to create a class that implements IConfigurationProvider, which declares methods to load, store, and retrieve configuration values:

To make things even easier, ASP.NET 5 has an abstract ConfigurationProvider class that implements IConfigurationProvider using a Dictionary<string, string> to store its values:

We can inherit from this class, override the Load method, and store our settings using the Data property:

In this example we use hard-coded values, but you can imagine retrieving configuration values from a database or other external source.

To keep with convention, we will also define an extension method to use our custom provider:

In our Startup class, we add our custom provider after loading the appsettings.json file, and then display the configured values when we receive a request:

For this example, we’ll use this appsettings.json file:

Starting the application and browsing to it, we see the values from both the appsettings.json file and our custom provider:


You can download source code for the example here:

Leave a Reply