Published: 20 Sep 2006
By: Brian Mains

.NET 2.0 produced a new way to create custom configuration sections, one that is even easier than before. It involves a custom class that inherits from the ASP.NET ConfigurationSection class, defining usable properties that map to the properties for the configuration element.

Introduction

ASP.NET 2.0 provides a new way to create custom configuration sections. This uses the ASP.NET 2.0 ConfigurationSection class to expose properties that represent the attributes in the web configuration element. It uses a Properties collection that is similar to the StateBag used for viewstate to read/write property values. It is possible to write to the configuration settings; however, this article will only talk about reading.

To expose the properties in the collection, you define properties with get/set accessors. However, you do it different. Let's look at the class:

Let's look at this in a little more depth. Above, we have several ConfigurationProperty objects, which represent the properties in the class and attributes in the configuration file. There are two ways to declare properties; either through creating the properties and add them to the Properties collection in the constructor, or using a ConfigurationProperty attribute (as with DisplayInformation).

Next, we use these properties through the default collection property. This accesses the actual property and returns the value as an object. Also, note the Current property, which returns an instance of the section to the code that is calling it, with all of the instantiated properties pulled in from the connection string.

Lastly, note that there are validators defined in the attribute; there are several validators that can be used, such as StringValidator, IntegerValidator, LongValidator, etc. In this I use a string validator, which defines the characters not allowed in it, the minimum character count, and the maximum character count.

What does it look like? The final output looks like this: Name and Email are required, so I provided them. These are set through the Name/Email properties, and so, when I render this code:

The output was:
Author: Fasdfl Fsafdsdf
Address: 111 Fake St., Harrisburg, PA 17105
Email: asdfasdf@yahoo.com
Display?: True

The address and display properties return the defaults, but the name and email properties use the web.config version. What happens if I don't provide the Name required attribute? You get a run-time error. Note that it doesn't even use my default defined in the code above; it instead provides an error.

Summary

Custom ConfigurationSection classes make it easier to define custom configuration sections, which are very useful in your applications.

References

See the MSDN documentation on ConfigurationSection.
Continue reading and see our next or previous articles Next Article >>

About Brian Mains

Brian Mains is an application developer consultant with Computer Aid Inc. He formerly worked with the Department of Public Welfare. In both places of business, he developed both windows and web applications, small and large, using the latest .NET technologies. In addition, he had spent many hou...

This author has published 73 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Custom Configuration Collections
Custom configuration sections support the use of collections. These collections are similar to the ...
Custom Provider Configuration Sections
Each provider you see in the framework has a custom configuration section associated with it, where ...
Configuration Section Validators
.NET 2.0 has a new means to create custom configuration sections. It also provides a means to valida...

You might also be interested in the following related blog posts


Introducing RadScrollablePanel for Windows Forms read more
Introducing SharePoint 2010 Training at U2U read more
Designer Support for One-Way navigations in Entity Framework 4 read more
How To: Silverlight grid hierarchy load on demand using MVVM and RIA services read more
Migrated from Community Server to DasBlog read more
Free software for you! WebsiteSpark let the mountain go to Microsoft instead. read more
Scenarios for WS-Passive and OpenID read more
Customizing the SharePoint ECB with Javascript, Part 3 read more
Querying a Multi-Tenant Data Architecture read more
Client Configuration in WCF 4.0 read more
Top
 
 
 

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.