AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=82&pId=-1
Creating a Web.config Editor - Part 1
page
by Jason N. Gaylord
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25844/ 42

Before We Begin
At this point, it is expected that the .Net Framework is installed and configured for ASP.Net applications. It is also expected that you have a web.config file already created in your web project. If not, copy the "default" web.config file below into notepad and save in the root of your web project as web.config.
Overview
Before we begin, you will need some understanding of what the web.config file is. The web.config file is a web configuration file written in XML. It contains several default parameters of your web site. It can also contain custom parameters.
Section 1: Opening The Web.Config File
Let’s begin by first opening up a default web.config file. Usually, your default web.config file looks like this:
Web.Config
**************************************************

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <system.web>
          <!--  DYNAMIC DEBUG COMPILATION
                Set compilation debug="true" to insert 
                debugging symbols (.pdb information)
                into the compiled page. Because this 
                creates a larger file that executes more 
                slowly, you should set this value to 
                true only when debugging and to false 
                at all other times. For more information, 
                refer to the documentation about 
                debugging ASP.NET files.    
          -->
          
          <compilation defaultLanguage="vb" debug="true" />
          
          <!--  CUSTOM ERROR MESSAGES
                Set customErrors mode="On" or "RemoteOnly" 
                to enable custom error messages, "Off" to 
                disable. Add <error> tags for each of the 
                errors you want to handle.
          -->
          
          <customErrors mode="RemoteOnly" />
          
          <!--  AUTHENTICATION 
                This section sets the authentication 
                policies of the application. Possible 
                modes are "Windows", "Forms", "Passport" 
                and "None"
          -->
           
          <authentication mode="Windows" />
           
          <!--  AUTHORIZATION
                This section sets the authorization 
                policies of the application. You can 
                allow or deny access to application 
                resources by user or role. Wildcards: "*"
                mean everyone, "?" means anonymous 
                (unauthenticated) users.
          -->
          
          <authorization>
              <allow users="*" />
              <!-- Allow all users -->
              <!--  <allow  users="[comma separated 
                    list of users]" 
                    roles="[comma separated list 
                    of roles]"/>
                    <deny users="[comma separated list 
                    of users]" roles="[comma separated 
                    list of roles]"/>
              -->     
          </authorization>
          
          <!--  APPLICATION-LEVEL TRACE LOGGING
                Application-level tracing enables trace 
                log output for every page within an 
                application. Set trace enabled="true"
                to enable application trace logging. If 
                pageOutput="true", the trace information 
                will be displayed at the bottom of each 
                page. Otherwise, you can view the 
                application trace log by browsing the 
                "trace.axd" page from your web 
                application root.
          -->
          
          <trace enabled="false" requestLimit="10" 
              pageOutput="false" traceMode="SortByTime"
              localOnly="true" />
              
          <!--  SESSION STATE SETTINGS
                By default ASP.NET uses cookies to 
                identify which requests belong to a 
                particular session. If cookies are not 
                available, a session can be tracked by 
                adding a session identifier to the URL. 
                To disable cookies, set sessionState 
                cookieless="true".
          -->
          
          <sessionState mode="InProc" 
              stateConnectionString="tcpip=127.0.0.1:42424"
              sqlConnectionString="data source=127.0.0.1;
              user id=sa;password=" cookieless="false" 
              timeout="20" />
              
          <!--  GLOBALIZATION
                This section sets the globalization 
                settings of the application.
          -->
          
          <globalization requestEncoding="utf-8" 
              responseEncoding="utf-8" />
      </system.web>
</configuration>

Notice how this file has a few different node levels. The first level is the <configuration> node. The second node is the <system.web> node. So, we have to pass by the first two node levels to get to the "good stuff" in the third level.
Section 2: Prepare Our Page
We must first prepare our document for this type of editing like shown below:
EditConfig.aspx
**************************************************

1:   <%@ Page Language="vb" %>
2:   <%@ import Namespace="System" %>
3:   <%@ import Namespace="System.Xml" %>
4:   <script runat="server">
5:   
6:       Sub Page_Load(s as Object, e as EventArgs)
7:          Dim myConfig As New XmlDocument()
8:          Dim myAttColl As XmlAttributeCollection
9:          Dim myAttribute As XmlAttribute
10:    
11:         myConfig.Load("C:\InetPub\wwwroot\web.config")
12:           
13:         If not isPostBack then
14:              Dim myCurrentValue as String
15:              Dim myListItem as ListItem
16:          
17:              myCurrentValue = myConfig.SelectSingleNode(
                     "configuration/system.web/customErrors/@mode").Value
18:              myListItem = node01.Items.FindByText(myCurrentValue)
19:    
20:              node01.SelectedIndex = node01.Items.IndexOf(myListItem)
21:           Else
22:              'We will save this for later!
23:           End If
24:        
25:        End Sub
26:    
27:    </script>
28:    <html>
29:    <head>
30:        <title>Web.config Editor</title>
31:    </head>
32:    <body>
33:    
34:    <form runat="server">
35:        <asp:Label id="title01" runat="server" text="Custom Errors:" />
36:        <asp:DropDownList id="node01" runat="server">
37:          <asp:ListItem Value="On" text="On" />
38:          <asp:ListItem Value="Off" text="Off" />
39:          <asp:ListItem Value="RemoteOnly" text="RemoteOnly" />
40:        </asp:DropDownList><br />
41:    </form>
42:    
43:    </body>
44:    </html>

Before we continue, let me explain each critical line. Lines 7-9 set up our XML variables. These "guys" are the ones that will conatin the xml properties. For this example, we won't use the XmlAttributeCollection (line 8). Line 11 loads the XML file into memory. The XML file in this case is the web.config file. Line 17 is where the actual node is loaded into the application. The SelectSingleNode method selects only a specific node in the web.config file. In this instance, the method selects the root ("configuration/system.web/") and then selects the node and attribute ("customErrors/@mode"). Finally, because this value is returned as an XmlAttribute type, we must get the value to convert it into a string value. Line 18 searches for the index value of the particular text in the drop-down control below. In the default web.config, the index will be returned as 2. Line 20 then sets the selected value of the drop-down to be equal to the index value returned in line 18. The drop-down list from lines 36-40 show the possible values for the customErrors node in the web.config file.
Section 3: Implementing The Page
Now that our document is set, we must then add code to edit this node. In this case, we will use a drop down box with the correct values of this node. We will then edit the web.config file by setting the value equal to the returned value of the drop-down control. Finally, we need to save the web.config file to store our new settings. An example of this is shown below:
EditConfig.aspx
**************************************************

1:   <%@ Page Language="vb" %>
2:   <%@ import Namespace="System" %>
3:   <%@ import Namespace="System.Xml" %>
4:   <script runat="server">
5:   
6:       Sub Page_Load(s as Object, e as EventArgs)
7:          Dim myConfig As New XmlDocument()
8:       
9:          myConfig.Load("C:\InetPub\wwwroot\web.config")
10:        
11:           If not isPostBack then
12:              Dim myCurrentValue as String
13:              Dim myListItem as ListItem
14:        
15:              myCurrentValue = myConfig.SelectSingleNode(
                    "configuration/system.web/customErrors/@mode").Value
16:              myListItem = node01.Items.FindByText(myCurrentValue)
17:        
18:              node01.SelectedIndex = node01.Items.IndexOf(myListItem)
19:           Else
20:             'We will save this for later!
21:           End If
22:        
23:        End Sub
24:        
25:        Sub Button_Click(s as Object, e as EventArgs)
26:           Dim myConfig As New XmlDocument()
27:           Dim myAttribute As XmlAttribute
28:        
29:           myConfig.Load("C:\InetPub\wwwroot\web.config")
30:           
31:           myAttribute = myConfig.SelectSingleNode(
                 "configuration/system.web/customErrors/@mode")
32:           myAttribute.Value = node01.SelectedItem.ToString()
33:           
34:           myConfig.Save("C:\InetPub\wwwroot\web.config")
35:        End Sub
36:    
37:    </script>
38:    <html>
39:    <head>
40:        <title>Web.config Editor</title>
41:    </head>
42:    <body>
43:        <form runat="server">
44:          <asp:Label id="title01" runat="server" text="Custom Errors:" />
45:          <asp:DropDownList id="node01" runat="server">
46:            <asp:ListItem Value="On" text="On" />
47:            <asp:ListItem Value="Off" text="Off" />
48:            <asp:ListItem Value="RemoteOnly" text="RemoteOnly" />
49:          </asp:DropDownList>
50:          <br />
1:          <asp:Button id="myButton" runat="server" Text="Update"
                 OnClick="Button_Click" />
52:        </form>
53:    </body>
54:    </html>

Now that we have changed a pre-defined node, let's change a custom node. This node will have a similar node, but with a different property. This will be demonstrated in Part 2. You can go to Part 2 by clicking here.

Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 5:17:41 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search