Creating a Simple Configuration Section Handler Using ASP.NET 2.0
page 3 of 6
by Jason N. Gaylord
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 61189/ 432

Adding the Handler to the web.config File

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>

View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 8 and type the answer here:

User Comments

Title: Saved my day!   
Name: Vijay
Date: 9/10/2008 4:51:25 PM
Comment:
Was wrestling with plethora of related .NET classes on the MSDN site trying to get them to work. You just saved my day! Thanks!
Title: Getting it to work for console applications   
Name: RustInRivers
Date: 8/9/2007 3:56:01 PM
Comment:
Hey renee I ran into the same problem when trying to get it to work in a console application. You have to use the config section in Listing 3 where the assembly is specified.

type="MyApplication.MyCustomConfigurationHandler,
MyApplication"

The last MyApplication would be the name of your console application.
Title: Great walkthrough   
Name: Eran Nachum
Date: 5/30/2007 9:03:41 AM
Comment:
Jason, very good step by step explanations, helped me a lot.
You invited to check my weblog sometime at http://www.eranachum.com
Title: Write to configuration   
Name: Linus Joseph
Date: 1/30/2007 4:52:12 PM
Comment:
Daniel Archer,

Here is how to write to configuration. In fact i have added a tool to manage configuration

http://blogs.covarius.com/PermaLink,guid,4b1fe1b7-883e-444b-92f9-f8c4758d6c79.aspx
Title: Good article, but can you write to the configuration?   
Name: Daniel Archer
Date: 10/19/2006 12:14:37 AM
Comment:
Hi, great example, but I want to be able to write values to the configuration, can I do this?
Title: good   
Name: renee
Date: 10/6/2006 6:32:51 PM
Comment:
worked in web.config but not in windows console app. I get following error-
An error occurred creating the configuration section handler for customSection/settings: Could not load type 'MyCustomConfigurationHandler' from assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Title: Great Work!   
Name: Mumtaz Ali Sarki
Date: 9/7/2006 7:24:17 AM
Comment:
This really a great example for creating Configuration Sections.
Title: Beautiful Example   
Name: Vikram
Date: 9/5/2006 1:06:31 AM
Comment:
This is beautiful example of how to Create the Configuration Handler
Thanks
Title: Great starting point!   
Name: John Mencias
Date: 8/30/2006 6:40:13 PM
Comment:
Muchas gracias. This provided me with a simple but solid starting point. Maybe you can now do an article about Advanced Configuration Handlers where instead of attributes being mapped to namevalue pairs we have xml nodes being mapped to objects. In other words, properties are returned as objects rather than simple strings or ints. Again, thanks!
Title: Great description   
Name: Mikhail Panshenskov
Date: 8/29/2006 9:58:22 PM
Comment:
Thanx, man!
Title: More information   
Name: Lotfi Zrikem
Date: 8/26/2006 10:46:42 AM
Comment:
Thanks for your example very helpful. I wanna know how can i loop over elements il my section and construct a collection of elements ?
Thanks a lot
Title: Very Gud   
Name: Umesh V
Date: 8/23/2006 1:28:07 AM
Comment:
very useful example. can be used in all the projects where we can stop using everything from AppSettings.
Title: Really Great One   
Name: sanu
Date: 8/21/2006 1:32:31 AM
Comment:
This is the one of the best example to start with Configuration Section Handler. Its really helpfull.
Thank yaar.
Keep it Up!!
Title: @tomgron   
Name: sudhan
Date: 8/17/2006 10:21:28 AM
Comment:
It does work on WinForms. I actually used it in a Windoes Console Application using C#, because that what I was required to do. And as the author mentioned, its tough before you figure it out, I couldnt find such nice doc anywhere else.

Really cool
Title: Great!   
Name: peter
Date: 8/11/2006 1:46:06 PM
Comment:
I'm gonna try it and put it in our new project! :)
Title: Excellent   
Name: tomgron
Date: 8/11/2006 10:40:35 AM
Comment:
I think this is excellent example of using different configuration sections in ASP.NET, although I guess this might run on WinForms apps with very little modifications as well. And as this can be used in order to configure and use plugins in applications, the use of this kind of thing is endless. I have previously tried to follow the code in DotNetNuke where this functionality has been implemented, but without luck since it's done originally on top of .NET 1.1 and has rather complex structure as most of functions that are built in in ASP.NET 2.0 were coded into DotNetNuke's framework.

Good job !

Product Spotlight
Product Spotlight 






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/8/2009 3:41:54 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search