In this article, a simple web based editor is built for the web.config file of ASP.NET. This article will cover the basics of the new web.config file structure, as well as helping the reader to understand the configuration file hierarchy of the .NET environment. It will also demonstrate how the XmlDocument object and its methods are used to simply load, modify, and write out XML from a .aspx page . The final code download is a simple web configuration file editor that allows a user to change the configuration settings of a web application through another web application, without needing to have explicit knowledge of the web.config file and it's format.
Code Design Motivation
In order to edit the web.config file through the web, the page performing the configuration work needs to be in a separate web application or
Application Domain from the web application for which it is changing the configuration settings. This is because of the way ASP.NET deals with configuration file changes. Recall from the explanation above, that when ASP.NET detects changes in the web.config file, it reconfigures itself according to those changes, but allows current requests to finish using the current configuration settings. Given this, it follows that the file performing the configuration would benefit by not changing along with the new configuration settings, as it becomes hard to detect the new changes. This is not to say that the simple web configuration tool will only work in it's own application domain, but results may appear non deterministic at times, and therefore a bit more difficult to debug.
Figure 1 illustrates the environment of the web configuration tool as it was developed and tested. A web application with the alias " webconfig " points to a directory containing the configuration editing aspx code. A second web application with the alias " testsite " points to a directory representing the production web application to be configured. The WebConfigEdit.aspx code will load, edit, and write out the web.config for the test site using the simple web based interface of the aspx page.
WebConfigEdit.aspx Control Modes
The WebConfigEdit.aspx page works in two modes, normal and postback. In normal mode, the code simply initializes form elements with the current settings in the web.config file and displays them as choices to the user. In postback mode, the code reads in the user selections and applies those selections to the web.config file. The following code illustrates the overall control flow of the WebConfigEdit.aspx code.
void Page_Load(Object sender, EventArgs e){
. . .
//code omitted for clarity
. . .
//If the page is in postback mode, update the config file
if(Page.IsPostBack){
UpdatePageItems(ref objXML);
UpdateTraceItems(ref objXML);
UpdateCustomErrors(ref objXML);
objXML.Save(CONFIG_FILE);
}else{
//else use the config file to initialize the form elements
InitializePageItems(objXML);
InitializeTraceItems(objXML);
InitializeCustomErrors(objXML);
}
}
Loading web.config
Loading the web.config file from as aspx page is a very straightforward operation that relies on the fact that the configuration file is a well-formed XML document. The XmlDocument class in the System.Xml namespace has a simple Load(string) method that allows an XML document to be parsed and loaded into memory given a file path as a string. This allows the web.config file to be loaded into an XML DOM that the code can now search and edit easily using the DOM interface. The following C# code in the Page_Load method loads the web.