The ASP.NET Web.config file is a configuration file which holds the settings for a IIS driven web application. The ASP.NET Web.config file contains information that control module loading/integration, security configuration, session configuration, application language and compilation settings. Before going into the details of the ASP.NET Web.config, we will first discuss common scenarios to utilize the web.config file.
There are many occasions where we need to store global variables in our web applications. These variables are extensively used in many pages of the application. In the classes of each page, we can create static variables to be used as a global variable. This would end up being redundant. We can also create a separate class containing common global variables. Only problem in storing it in a global shared class is that any change would require recompilation. Instead of hard coding these variables to different classes or even a shared class, we can store them globally in the web.config file. By storing them on the ASP.NET Web.config, we can easily change the settings of the application without changing the source code and without recompilation.
Let's break it down into a scenario. Imagine you had a site with multiple pages, multiple user controls, and classes of source code which pointed to a database named Database1. If you were to move to a new database called Database2, we would have to manually change all the pages, classes and controls to the new database name. This would then need to be recompiled and hosted to work. By using the web.config we can store it in a central location establishing easy changeable settings.
Other reasons we would use the web.config is to
enable/disable debug modes
allow impersonation of users
authentication
authorizatoin
configure our web application to handle different types of errors.
What is the Web.Config file?
The web.config file is nothing more than a xml file which holds our IIS web application settings. These settings are used by the IIS and our web application and any virtual directories that have other asp.net web applications. The root of this xml has to be the configuration tag.