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:
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:
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: