Reusing Configuration Files in ASP.NET Core
The release of ASP.NET Core 1.0 has enticed existing ASP.NET customers to migrate their projects to this new platform. While working with a customer to move their assets to ASP.NET Core, we encountered an obstacle. Our customer had a many configuration files (
*.config) they used regularly. They had hundreds of configuration files that would take time to transform into a format that could be consumed by the existing configuration providers. In this post, we’ll show how to tackle this hurdle by utilizing ASP.NET Core’s configuration extensibility model. We will write our own configuration provider to reuse the existing
The new configuration model handles configuration values as a series of name-value pairs. There are built-in configuration providers to parse (XML, JSON, INI) files. It also enables developers to create their own providers if the current providers do not suit their needs.
Creating the ConfigurationProvider
By following the documentation outlined in Writing custom providers, we created a class that inherited from
ConfigurationProvider. Then, it was a matter of overriding the
public override void Load() function to parse the data we wanted from the configuration files! Here’s a little snippet of that code below.
The ConfigurationProvider in Action
Consider the following Web.config:
to reuse this file, all we have to do is use our new provider like below and run
dotnet run to see the code in action.
Our configuration provider code is on GitHub so you can check it out yourself and see how easy it is to use the ASP.NET configuration model.