Download sample code |
In this article, we would see the method to update the key parameters in configuration file without causing the application instance.
This article assumes that the reader is already familiar with ASP.NET programming.
Keywords
web.config: Application configuration files contain settings specific to an application. This file contains the configuration settings that the common language runtime reads (such as the assembly binding policy, remoting objects, and so on), and settings that the application can read [MSDN].
xml: Extensible Markup Language (XML) is the universal format for data on the Web. XML allows developers to easily describe and deliver rich, structured data from any application in a standard, consistent way. XML does not replace HTML; rather, it is a complementary format [MSDN].
Requirement
According to the requirement we need to modify the values of the keys found in web.config programmatically or by providing configuration screen and also without re-instantiating Application instance (i.e. Application instance is not lost). So the user can modify the values of the keys for which he is allowed to do so.
Description
We all are familiar with ASP.Net programming and also the production environment of the website under Internet Information Services (IIS).
During the application life cycle, the application raises events that you can handle and calls particular methods like Application_Start and Application_End placed in Global.asax in the root directory of your application.
The Application_Start and Application_End methods are special methods that are called once for the lifetime of the application domain.
Here are some of the conditions in which the Applicaton_Start event is invoked.
- On restarting of IIS.
- Application pool of the website is restarted.
- Updating the web.config file.
Add the new xml file in your solution by selecting Add New Item > Web Configuration file from dialog box.
Name this file whatever you like. Here I named it as “DataKey.config”.
Web.config
In the above code snippet we have used configSource attribute of the appSettings tag. ConfigSource take the path of the another config file in the solution. And during the runtime the keys from config file are replaced in the appSettings section in web.config file.
DataKey.config (custom configuration file)
Here there is no need for XML declaration, but the document element should be same as that we provide in web.config.
C# Code
System.Configuration.ConfigurationManager.AppSettings["merchantid"] System.Configuration.ConfigurationManager.AppSettings["merchantkey"]Accessing the value of the key is same way we access from web.config.
C# Code
System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); xml.Load(Server.MapPath(@"~\DataKey.config")); System.Xml.XmlNode node = xml.SelectSingleNode("//appSettings/add[@key='" + txtKeyName.Text + "']"); node.Attributes["value"].Value = txtKeyValue.Text.Trim(); xml.Save(Server.MapPath(@"~\DataKey.config"));Updating DataKey.config can be done using methods of System.Xml.XmlDocument.
So here the keys can be saved in XML file and can be modified without loosing the Application instance.
Hence we are done!!!
No comments:
Post a Comment