Web.config file configures fundamental settings


Every web application in ASP.NET includes a web.config file that configures fundamental settings from error message to security.
It uses a predefined XML format. THe entire content of the file is nested in a root<configuration> element.The elementary structure of the Web.config
is:-

<?xml version="1.0" ?>
<
configuration>
<
configSections>...</configSections>
<
appSettings>...</appSettings>
<
connectionStrings>...</connectionStrings>
<
system.web>...</system.web>
<
system.codedom>...</system.codedom>
<
system.webServer>...</system.webServer>
</
configuration>

remember it is case sensitive, like all XML documents.
Now it has <appSettings> section allows us to add our own miscellaneous pieces of information.
The <connectionStrings> section allows us to define the connection information for accessing a database. Finally, the <system.web> section holds every ASP.NET setting we’ll need to configure.
Inside the <system.web> element are separate elements for each aspect of website
configuration. we can include as few or as many of these as we want.Here coms the concept of nested configuration.
It uses multilayered configuration.
Storing custom setting in the web.config file
using neste <appSetings> in the root <configuraion>element
here is the basic :-

<?xml version="1.0" ?>
<
configuration>
...
<appSettings>
<!--
Custom application settings go here. -->
</
appSettings>
...
<system.web>
<!--
ASP.NET Configuration sections go here. -->
</
system.web>
...
</configuration>

Path is defined for storing important information as such:-

<appSettings>
<
add key="DataFilePath" value="e:\NetworkShare\Documents\WebApp\Shared" />
</
appSettings>
Down is the simple test page for quering and displaying result:-

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
public partial class ShowSettings : System.Web.UI.Page
{
protected void Page_Load()
{
lblTest.Text =
"This app will look for data in the directory:<br /><b>";
lblTest.Text +=
WebConfigurationManager.AppSettings["DataFilePath"];
lblTest.Text +=
"</b>";
}
}


Similar Articles