Creating Section Groups ASP Help

A section group as the name suggests allows configuration sections to be grouped together. The main reason for using section groups is to add further structure to the data in the configuration files in order to make it clear that section groups are related to one another and affect related parts of the application. Listing 9-20 shows the addition of a section group to contain the newUserDefaul ts and places sections in the Web.config file. I have also added the definition of the section group in the configSections element.

You can see from the listing that a section group is a custom element that wraps one or more custom sections. The sectionGroup element tells ASP.NETwhich handler class will represent the section group and defines the relationship between the group and the sections it contains. In the listing, my section group is called customDefaults, the handler class is called CustomDefaul ts, and the section group will contain the newUserDefaul ts and places sections that I created earlier.

Creating the Section Group Handler Class

Section groups rely on handler classes, similar to the ones used for configuration sections. For my custom Defaults section group, I added a class file called CustomDefaul ts. cs to the Infrastructure folder and used it to define the class shown in Listing 9-21.

Listing 9-21. The Contents of the CustomDefaults.cs File

The purpose of the section group handler class is to expose properties that access the configuration sections that the group element contains. Section group handler classes are derived from the ConfigurationSectionGroup class, which defines a Sections property through which instances of section handler classes can be obtained by name, like this: