Retrieving Settings

Many of the settings used within the web.config and machine.config files simply modify the way that ASP.NET works. Others enable us to dynamically customize our applications based on many factors. In order to customize our applications, we have to be able to retrieve settings from the configuration.

When we retrieve data, we are not retrieving it from any specific configuration file, but from the cached configuration.This cached configuration includes all inherited configuration and any location specific configuration information. For the examples used in this section, we will be using the "TestConfig" application that we designed the preceding web.config file for.

ASP.NET exposes intrinsic static methods for some configuration options. An example of this is the Session method.When you have set session state configuration by using the <sessionState> tag, you can read this in by using the Session method.This process is shown in the following code sample:

Dim nocookies As Boolean = Session.Cookieless

The second method of retrieving settings is only applicable to settings configured using the <appSettings> tag.To retrieve these settings, you simply use the ConfigurationSettings.AppSettings method and supply the key.This method will return the value stored under that keyname.This method is shown in the following code sample:

Dim myvalue As String = ConfigurationSettings.AppSettings("mykey")

You can use the final retrieval method to obtain any value within the configuration. This is the ConfigurationSettings.GetConfig method. In order to use this, you must know the exact path to the configuration setting that you wish to retrieve.The syntax for this method is shown in Figure 4.3.This code is available on the included CD as testconfig.aspx.

Figure 4.3 Application (TestConfig.aspx)

<script language="VB" runat="server">

Public tblBack As String = "" Public tblFore As String = ""

Figure 4.3 Continued

Sub Page_Load(source As Object, E As EventArgs) dim config As NameValueCollection= ConfigurationSettings.GetConfig ("testConfig.group/mainAppSettings")

dim strTblBack as string = config("tableBackgroundColor") dim strTblFore as string = config("tableForegroundColor")

if strTblBack <> nothing then tblBack=strTblBack else tblBack="lightgreen" end if if strTblFore <> nothing then tblFore=strTblFore else tblFore="purple" end if

End Sub

<title>Test Configuration</title>

<table border=1 bgcolor=<%=tblBack%> bordercolor=<%=tblFore%>> <tr>

<td>Important</td>

Continued

Figure 4.3 Continued

<td>Some</td> <td>More</td> <td>Data</td> </tr> </table> </body> </html>

This code uses configuration sections that we defined within our web.config file in the previous section.The full possibilities of this configuration can be realized by first running the code in its own virtual directory, and then running the same code within an "execreports" subdirectory.This will show you how the configuration options change based on the <location> tag.

0 0

Post a comment