Creating a Configuration Class
There is a better mechanism for reading in values in the web.config file. This mechanism takes advantage of ASP.NET's ability to call any class you specify to read values from the web.config file. You do this by using settings within the web.config file.
There are many benefits to using this class: You get an IntelliSense-aware list of properties for each value you specify, each property has a specific data type and, finally, if any values have changed in the web.config file the values are automatically refreshed.
While it does take a little time to learn how to create a configuration class, the steps are not difficult and you will find the payoff in increased functionality and reusability well worth the effort.
Overview of Steps
There are some basic steps that you must perform to create a configuration class that can be automatically reloaded with values. Below is a list of general steps to create a configuration class.
- Create a <configSections> element in the web.config file.
- Add a <section> element with some attributes that describe the name of your configuration class and where it is located.
- Add your own <config> element with your own user-defined keys and values to read.
- Create a Class that implements the IConfigurationSectionHandler interface.
- Write the appropriate implementation of the Create method in your class.
- Create an initialization method to be called from the Application_Start event in Global.asax.
- Add a call to an initialization method in your class from the Application_Start event procedure in Global.asax.
- Add properties that correspond to each of the keys and values you created in your configuration section in web.config.
After you have done these steps, it is a simple matter of adding properties to the class that correspond to the various keys that you create in your <config> element.
Changing the web.config File
The first step is to create a <configSections> element in the web.config file. You may add one or more of your own <config> sections depending on how you might want to categorize your settings.
Follow the steps below to create a sample project that demonstrates how to create a configuration class that can dynamically change values.
- Create a new ASP.NET Web Application project in Visual Studio .NET. Set the name to ConfigSample.
- Open web.config and add the XML, shown in Listing 1, immediately below the <configuration> tag.
In the <configSections> element, add a <section> tag to define the name of the next element. In this example, it is called AppConfig
. This means that you will create another section with the <AppConfig> element somewhere in the web.config file. In addition, you need to define the full namespace and class name of the class you create to read the data from this configuration section. In this example it is called ConfigSample.AppConfig. ConfigSample is the name of this project (and thus the name of the namespace), AppConfig is the name of the class you will create in this project to read the values from the AppConfig section. After this you add a comma and then repeat the namespace one more time.
You can define as many <add key=> tags in the <AppConfig> element as you want. For each one of these tags, you will most likely create a public property to expose these from the AppConfig class. In the example shown in Listing 1
you have two keys: ConnectString and SiteName. (See Sidebar 2
: What Else is in web.config?