ASPAlliance.com : The #1 ASP.NET Developer Community : Web Services
ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
ASP.NET Components Toolkit
ASP.NET Components Toolkit

Find Prices


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software
Jason Gaylord's ASP Corner
Creating a Web.config Editor - Part 1
Jason Gaylord (jgaylord@aspalliance.com)



COMING SOON: Web.Config Editor Application
For more information, go here.

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.

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.

Let’s begin by first opening up a default web.config file. Usually, your default web.config file looks like this:

<?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
ach 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. We must first prepare our document for this type of editing like shown below:
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.

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:
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 />
51: <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. Go to Part 2 by clicking on the link provided here: [Part 2]

Related Resources
Simple XSLT&XPath grouping (Teemu Keiski)

[My Column]   [More XML Articles]   [Printer-Friendly]
 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/21/2009 6:03:11 PM