Browse DevX
Sign up for e-mail newsletters from DevX


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

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Create the AppConfig Class
Now that you have the configuration section created, it is time to create the AppConfig class. There are three important items that you must do to create this class correctly. First, you must implement the interface IConfigurationSectionHandler (for more information on implementing interfaces see ".NET Interface-based Programming" in the May/June issue of CoDe Magazine). Second, you must create an Init method. Third, you must implement the Create method from the IConfigurationSectionHandler interface. Refer to Listing 2. The Init Method
You need to create a method within the AppConfig class to call from the Application_Start event procedure in the Global.asax page. This initializes the "AppConfig" configuration section of the web.config file. Refer to Listing 3.

The Create Method
The Create method is called the first time you start an application or anytime that the web.config file is modified. This forces your shared property values to be reloaded with the current values from the web.config file. Refer to Listing 4. Change Global.asax
From the Application_Start event procedure in the Global.asax you must call the Init method in the AppConfig class. This method loads the Configuration section named "AppConfig" into a global memory space within the ASP.NET engine. Refer to Listing 5.

Comment and Contribute






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



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