Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ASP.NET: Creating an Application Configuration Class : Page 2

Most of you are probably aware that the web.config file in an ASP.NET project controls the behavior of your Web site. If you make a change to one of the built-in settings in this file, ASP.NET automatically detects those changes and applies them immediately. Wouldn't it be nice if you could have your own settings in this file applied immediately as well? In this article you will learn how to do just that. You will also learn the difference between the Application object and creating your own Configuration class.


advertisement
Creating a Configuration Class
There is a better mechanism for reading in values in the web.config file. This mechanism takes advantage of ASP.NET's ability to call any class you specify to read values from the web.config file. You do this by using settings within the web.config file. There are many benefits to using this class: You get an IntelliSense-aware list of properties for each value you specify, each property has a specific data type and, finally, if any values have changed in the web.config file the values are automatically refreshed.

While it does take a little time to learn how to create a configuration class, the steps are not difficult and you will find the payoff in increased functionality and reusability well worth the effort. Overview of Steps
There are some basic steps that you must perform to create a configuration class that can be automatically reloaded with values. Below is a list of general steps to create a configuration class.

  1. Create a <configSections> element in the web.config file.
  2. Add a <section> element with some attributes that describe the name of your configuration class and where it is located.
  3. Add your own <config> element with your own user-defined keys and values to read.
  4. Create a Class that implements the IConfigurationSectionHandler interface.
  5. Write the appropriate implementation of the Create method in your class.
  6. Create an initialization method to be called from the Application_Start event in Global.asax.
  7. Add a call to an initialization method in your class from the Application_Start event procedure in Global.asax.
  8. Add properties that correspond to each of the keys and values you created in your configuration section in web.config.
After you have done these steps, it is a simple matter of adding properties to the class that correspond to the various keys that you create in your <config> element. Changing the web.config File
The first step is to create a <configSections> element in the web.config file. You may add one or more of your own <config> sections depending on how you might want to categorize your settings.


Follow the steps below to create a sample project that demonstrates how to create a configuration class that can dynamically change values.

  1. Create a new ASP.NET Web Application project in Visual Studio .NET. Set the name to ConfigSample.
  2. Open web.config and add the XML, shown in Listing 1, immediately below the <configuration> tag.
In the <configSections> element, add a <section> tag to define the name of the next element. In this example, it is called AppConfig. This means that you will create another section with the <AppConfig> element somewhere in the web.config file. In addition, you need to define the full namespace and class name of the class you create to read the data from this configuration section. In this example it is called ConfigSample.AppConfig. ConfigSample is the name of this project (and thus the name of the namespace), AppConfig is the name of the class you will create in this project to read the values from the AppConfig section. After this you add a comma and then repeat the namespace one more time. You can define as many <add key=> tags in the <AppConfig> element as you want. For each one of these tags, you will most likely create a public property to expose these from the AppConfig class. In the example shown in Listing 1 you have two keys: ConnectString and SiteName. (See Sidebar 2: What Else is in web.config?


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date