Free ASP.NET Hosting – How Configuration System Works in ASP.NET 5

With ASP.NET 5, we get new configuration files. Yes, files, because more than one config file format is supported. Our configuration can be JSON, INI or XML and I’m sure you can write configuration handlers that support your own custom formats. Also support for multiple configuration containers is here. In this posting, we will show how new configuration system works in ASP.NET 5.

Suppose we have three configuration files:

Config.json

Config.xml

 Config.ini

They all follow the same structure although formats are different.

Dependencies

Before we can load our config files we have to add some dependency definitions to project.config file:

Having these dependencies to configuration model we can read configuration from JSON and INI files.

Loading Configuration Files

Something that I have not covered yet is how ASP.NET vNext applications are loaded. I don’t stop on this topic but just mention that there is class called Startup with method Configure(). In this method you add all services you need to your application. In this method we will also load configuration files:

Now we have configuration files loaded and it’s time to get read some config parameters from our powerful configuration object.

Reading Configuration Parameters

Now let’s read all these configuration params from configuration object. I used Watch window in Visual Studio to show values of parameters.

aspnet-mvc6-config-paramsIf we want to read parameters of some other type than string then we can call generic version of Get() method:

Why Multiple Configurations?

Why we need configuration system like this? Well, there are some new scenarios that are supported through this system:

We can use multiple configuration containers, then use same type of settings files in web and desktop programs. To get better idea what I mean take a look at the following code:

Also you can use those configuration files in your custom components so developers who are using your components doesn’t have to load configurations in their code.

HostForLIFE.eu Free ASP.NET Hosting

Try their Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of their Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc.

hostforlife banner