Custom (nested) configuration for your C#/MVC application

configuration

Sometimes you want to store settings in a config file like web.config or app.config for your modules. In this small tutorial I will show you how to do that. I will not only show you how to define your own sections but also how to put this config in a separate file.

It actually doesn’t matter if you are building a MVC application or a console application. The way you create custom config sections is the same for both with the exception that for a MVC application you define them in the web.config and for console applications your define them in the app.config.

Lets say we have a budgetplanner MVC application and this application has 4 sections where you can create teams, customers, projects and budgets. Every section has its how pagination which can be configured in the web.config. You can configure for example the amount of items per page and how many pages should be visible in the pagination. A config section for this could look like this:

Lets start by putting this config section in the web.config file under the node
<configuration> … </configuration>. We also need to define this config section inside the node configsections. So your web.config will now look like this:

Important: For now lets have only one section with the name “teams” to keep it simple. Later we will add more then one section like above but this requires extra coding.

Take a closer look at the configsection we just added. The name attribute of the configsection is the name of the node. The node is <planner> so the name is planner. The type attribute points to the class which defines this section.

This is the point were it gets a bit more complicated. We like all configs to be strongly typed that’s why we need a class that defines the structure of our configsection.

So lets create a file PlannerSection.cs with an empty class like this:

This class is going to define our config section. The first node in our config is called section. I know that the name is a bit confusing here but remember this is an arbitrary name. We could also called it ‘page’ or ‘part’ or whatever your like. We need to define this node in our class like this:

For each node you need a property to get or set the node object and you need a node object that defines the attributes of the node. So the above property gives us access to the node object and returns and object called Section which defines the node attributes. So next we need to create this object.

Every section has a name so the section object has a property called name. Every section node also has a child node called pagination so it also needs a property called pagination to get this node object. Now lets also create a pagination object:

That’s it. Now we have fully defined the new configsection. You can access the config now from a controller like this:

How to support multiple <section> tags

Remember that our original config had multiple <section> tags each with its own name and child nodes. If you add another <section> tag now you will get an error when trying to load the config section ‘planner’. To support multiple <section> tags we need a ConfigurationElementCollection
wrapper. So replace the property Section in the class PlannerSection with a property Sections that looks like this:

And create a SectionCollection object/class

Now you can loop through all sections using this code:

Putting your config in an external file

This is really simple. Just replace the section:

with

In your web.config and create a new file Planner.config. This new file should then contain this:

More information

Leave a Comment.