Now that the handler has been created, we need to add a
reference to it in the web.config file. To do so we will add a new section
under <configuration> called <configSections>. This section lists
all configuration sections that we would like to add to the web.config file
besides the sections already specified in the machine.config. Within the
<configSections> section, we will add another new section called
<sectionGroup>. The <sectionGroup> section will also have a name
attribute that will need to be set. This defines the section group name that
we will use within our web.config file. Finally, we add one more section below
the <sectionGroup> called <section>. We will need to add the name
and type to this section. The name specifies the name of the section and the
type specifies the class declaration type for the section. An example of the
web.config file is shown is Listing 2.
Listing 2 - Adding a configuration section to the
web.config file
<configSections>
<sectionGroup name="customSection">
<section name="settings" type="MyCustomConfigurationHandler"
allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
Since we have placed our code file in the App_Code folder,
our type will only need to be our class name. However, this is not always the
case. Let us say we built our handler and placed it inside of the namespace
"MyApplication." We then compiled the code and placed it into our
bin file. The compiled DLL had a name of MyApplication.dll. In this case, our
configuration section would appear similar to the one in Listing 3.
Listing 3 - Adding a configuration section to the
web.config file if the configuration section is in a namespace and a compiled
DLL
<configSections>
<sectionGroup name="customSection">
<section name="settings" type="MyApplication.MyCustomConfigurationHandler,
MyApplication" allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
We can add some additional attributes to the type definition,
such as version number and locale.
Now that the configuration section has been added to the
<configSections> section, we can use it. Listing 4 shows an example of
how to use the section in the web.config file.
Listing 4 - Using the newly added section in the
web.config file
<customSection>
<settings firstNumber="20" secondNumber="43" />
</customSection>