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

Using the Configuration Section in Code

Now that we have our configuration section setup, we can use the new settings in our code.  In ASP.NET 2.0, Microsoft introduced a new class in System.Configuration called ConfigurationManager.  The ConfigurationManager class is very useful as it will allow us to retrieve and set nearly any value in our web application.  In our application we will use the GetSection method and pass in an XPath value.  An example of how to retrieve the settings in Visual Basic and C# are shown in Listing 5.

Listing 5 - Referencing values in the new configuration section

[Visual Basic .NET]

Dim config As New MyCustomConfigurationHandler
config = System.Configuration.ConfigurationManager.GetSection("customSection/settings")
Response.Write(config.FirstNumber.ToString())
Response.Write(config.SecondNumber.ToString())

[Visual C# .NET]

MyCustomConfigurationHandler config =
 (MyCustomConfigurationHandler)
System.Configuration.ConfigurationManager.GetSection("customSection/settings");
Response.Write(config.FirstNumber.ToString());
Response.Write(config.SecondNumber.ToString());

View Entire Article

User Comments

Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-07-02 10:11:22 AM
Comment:
http://www.jersey2shop.com
http://www.cheapjersey2store.com
http://www.jerseycaptain.com
http://www.yourjerseyhome.com
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes
Cheap NFL,NBA,MLB,NHL
,heap jerseys,2012 nike nfl Jerseys,nba jersey and shorts,oklahoma city thunder jersey,official jeremy lin new york knicks jersey,NFL Jerseys Wholesale,blake griffin jersey blue,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
,Wholesale cheap jerseys,Cheap mlb jerseys,]Nike NFL Jerseys,Cheap China Wholesae,Wholesale jerseys From China,2012 nike nfl Jerseys,Jerseys From China,,2012 nike nfl Jerseys,Revolution 30 nba jerseys,jersey of nba chicago bulls direk rose ,nfl jerseys,green bay packers jerseys wholesale,Buffalo Bills nike nfl jerseys sale,good supplier soccer jerseys,cool base mlb jerseys,Revolution 30 nba jerseys,2012 stanley cup nhl jersey,
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes. www.yourjerseyhome.com
Title: Thank you.   
Name: Gordon
Date: 2011-05-07 5:23:11 PM
Comment:
Wow, you wouldn't believe how hard it is to find such a clear way to implement a custom section handler. You rock.
Title: Thanks   
Name: Andre Albuquerque
Date: 2011-05-06 12:37:15 PM
Comment:
Thanks for your article. Simple enough to absorb the CustomConfigurationHandler thing.

I was having trouble with the
type= "MyApplication.MyCustomConfigurationHandler, MyApplication" part because MyApplication was in the form ABC-Program, so the Handler was converted to (note the _) ABC_Program.MyCustomConfigurationHandler and the .dll was ABC-Program.dll

Without your explanation I would have lost much more time, since the Exception thrown hadn't any useful details (System.Exception._COMPlusExceptionCode with the value -532459699).

Thanks again,

Andre
Title: Thanks RustInRivers   
Name: C.VENDEESHWARAN
Date: 2011-04-01 6:00:46 AM
Comment:
How to enable th debugging using asp.net
Title: Thanks RustInRivers   
Name: c.vendeeshwaran
Date: 2011-04-01 5:58:40 AM
Comment:
I want to be able to write values to the configuration, can I do this?
Title: Thanks RustInRivers   
Name: MrCricket
Date: 2010-09-28 11:03:25 PM
Comment:
That answer saved my a load of time
Title: Thanks!   
Name: coder
Date: 2010-05-25 3:30:53 PM
Comment:
Ditto that, RustInRivers - I was missing that name of the dll, because my original project was a web.config where it did not seem to have a problem, but creating a unit test app that used an app config required that extra specification, which I had missed.
Thanks!
Title: Excellent   
Name: Michel
Date: 2010-05-14 5:20:18 PM
Comment:
RustInRivers : you saved my day, i had the problem in a test project. I had to supply the name of the DLL (where you type 'MyApplication') where the object was in, in my case Project.Core, so it would be

type="Project.Core.Config.MyCustomConfigurationHandler,
Project.Core"

for me.

Thanks for getting me on track.
Title: hello   
Name: van le
Date: 2009-12-01 5:32:53 AM
Comment:
goog luck!
Title: Saved my day!   
Name: Vijay
Date: 2008-09-10 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: 2007-08-09 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: 2007-05-30 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: 2007-01-30 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: 2006-10-19 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: 2006-10-06 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: 2006-09-07 7:24:17 AM
Comment:
This really a great example for creating Configuration Sections.
Title: Beautiful Example   
Name: Vikram
Date: 2006-09-05 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: 2006-08-30 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: 2006-08-29 9:58:22 PM
Comment:
Thanx, man!
Title: More information   
Name: Lotfi Zrikem
Date: 2006-08-26 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: 2006-08-23 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: 2006-08-21 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: 2006-08-17 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: 2006-08-11 1:46:06 PM
Comment:
I'm gonna try it and put it in our new project! :)
Title: Excellent   
Name: tomgron
Date: 2006-08-11 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-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 10:29:31 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search