Understanding the Structure and Content of Web.Config File

There are many configuration settings that you require for your application. For example, mode of authentication you use or the database connection for your application. Instead of recording such configuration details in every page or at a static class, what if you have a single file in which you can store all configuration settings and use those settings across your application? You can achieve this using the configuration file named web.config file.

Structure of web.config file:

Web.config file is structured using XML. The file contains tags and each tag can have sub-tags and attributes associated with it. Here is a sample web.config file with basic configuration settings done:

<?xml version=’1.0” encoding=”utf-8”?>
<section name="SampleCustomTag"
type="System.Configuration.NameValueFileSectionHandler " />

<!—Include details about network classes, if any ?

<compilation defaultLanguage="c#" debug="false"/>
<customErrors mode="On" defaultRedirect="defaultCusErrPage.htm"/>
<authentication mode=”Forms”>
<forms loginUrl = "frmSampleLogin.aspx" timeout="20"/>
<allow users="User1, User2" />
<deny users=”*” />
<trace enabled="true" requestLimit="20" />
stateConnectionString=”tcpip =”
sqlConnectionString="data source=; Integrated Security=SSPI
timeout="40" >
<add type="sampleClass,sampleAssembly" name="sampleModule" />
<remove name="modulename"/>
<add verb="*" path="sampleFolder/*.aspx"
type="sampleHttpHandler,sampleAssembly" />
<httpRuntime appRequestQueueLimit="200" executionTimeout="500" />

<add key="DBConnString"
value="server=;uid=usr;pwd=pwd; database=DBPerson" />
</appSettings >

<sampleCustomTag >
<add key="sampleKey" value="sample key value" />
</sampleCustomTag >

As shown in the example above, web.config file has its root element as <configuration> tag. Inside this root element, following sections are allowed:

• configSections: If you want to create and use any custom tags in your web.config file, then you can define those custom tags using <section> tag inside <configSections>. In the above example, you have defined a custom tag called sampleCustomTag and that is used at the end of your web.config file as shown above. <sampleCustomTag> can define as many key value pairs that are required.

• system.net: If you have any network classes then the configuration settings for those classes can be done using system.net.

• system.web: Configuration settings required for your ASP.NET classes are defined inside <system.web> tags. Many sections or sub-tags are permissible inside this section. Most commonly used and most important sub-tags are mentioned in the above example. Purpose of each sub-tag mentioned in the example is discussed below:

o compilation: Using this tag you can specify what language you have used for coding and whether debugging has to be enabled in your application or not. This information will be used at the time of compilation. When debugging is set to true, performance of your application will be low.

o customErrors: When customErrors attribute called mode is specified as “On”, default error page specified in this tag will be displayed when any unhandled error occurs in the application. You can even customize error page based on error code and mention it in this tag using <error> sub-tag.

o authentication: Determines which authentication mechanism is being used by your application and also includes specific configuration details for that particular authentication. Example above is configured for forms authentication.

o authorization: This tag defines permissions for users to access your entire application or specific folder in your application. Example above means that only User1 and User2 can access the application and access is denied to all other users since wildcard indicator “*” is mentioned in deny tag.

o trace: Trace log for your entire application can be viewed using trace.axd utility or trace information can be viewed for each page by configuring in this tag.

o sessionState: HTTP Protocol you use for your ASP.NET application is stateless. However you can use sessions to maintain states. sessionState tag is used to configure sessions.

o httpModules: If your application has any specific httpModules, you can configure it using this tag. Here type specifies both class name and assembly name comma separated.

o httpHandlers: If you have any customized HTTP handlers for your application, then you can define it using this tag. Here type specifies both type of the handler and the assembly name comma separated.

o httpRunTime: Run time settings for your application can be configured using this tag. Above example specifies two runtime settings. appRequestQueueLimit specifies the maximum number of requests that can be queued in memory waiting for the server to process it. executionTimeout specifies the timeout period in seconds.

• appSettings: If you need specific data throughout the application, then you can define them as key value pair inside <appSettings> tag. You have defined a database connection string in <appSettings> tag above. You can access this connection string in your page using the following line of code:

string sampleConnString = (string )ConfigurationSettings.AppSettings["DBConnString"];

• Customized tags: Customized tags defined in configSections can be specified as an independent tag. For example, <sampleCustomTag> is a custom tag defined and used in web.config file shown above. The key “sampleKey” mentioned inside this custom tag can be accessed using the following line of code:



Add to My Yahoo!

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage


Recommended Resource

|How to Define Custom Error Pages for Error Handling In ASP.Net Application| Role of Web gardens and web farms in ASP.NET | Understanding Purpose and Usage of "event" Class Member Modifier in C# (C Sharp) | Understanding the Structure and Content of Web.Config File | Usage of C# (C Sharp) Query Keywords – group, by, into | Usage of C# (C Sharp) Query Keywords – join, on, equals, let |Usage of C# (C Sharp) Query Keywords – orderby, ascending, descending | Usage of C# (C Sharp) Query Keywords – select, from, where, in | Usage of Lambda Operator (=>) in C# (C Sharp) |Using C# (C Sharp) as a tool for object oriented programming | Using Dictionaries in .Net – An Overview | Using WCF for providing web service |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.