What is Web.config?

In classic ASP all Web site related information was stored in the metadata of IIS. This had the disadvantage that remote Web developers couldn't easily make Web-site configuration changes. For example, if you want to add a custom 404 error page, a setting needs to be made through the IIS admin tool, and you're Web host will likely charge you a flat fee to do this for you.
With ASP.NET, however, these settings are moved into an XML-formatted text file (Web.config) that resides in the Web site's root directory. Through Web.config you can specify settings like custom 404 error pages, authentication and authorization settings for the Web sitempilation options for the ASP.NET Web pages, if tracing should be enabled, etc.
The Web.config file is an XML-formatted file. At the root level is the For example, if we wanted to add a database connection string parameter we could have a Web.config file like so:
<configuration>
<!-- application specific settings -->
<appSettings>
<add key="connString" value="connection string" />
</appSettings>
<system.web>
...
</system.web>
</configuration>
Now, in any of your ASP.NET Web pages in this Web site you can read the value of the connString parameter like so:
ConfigurationSettings.AppSettings("connString")
To avoid this complication you can "group" your application's settings into a unique tag in the Web.config file. That is, you can create a tag named: <MyAppSettings> in Web.config and then use the as we did earlier to add application-wide settings.
To add a custom tag to Web.config you need to first explicitly specify the new tag name in Web.config via the <configSections> tag, like so:
<configuration>
<configSections>
<section name="MyAppSettings"
type="System.Configuration.NameValueFileSectionHandler,
System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</configSections>
...
</configuration>
This <section ... /> tag indicates that we are going to be adding a custom tag named MyAppSettings. Now we can add a <MyAppSettings> tag to our Web.config file and add <add ... /> tags to add application-wide parameters, like so:
<configuration>
<configSections>
<section name="MyAppSettings"
type="System.Configuration.NameValueFileSectionHandler,
System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</configSections>
<MyAppSettings>
<add key="connString" value="connection string" />

...

To read this custom value from an ASP.NET Web page we use the following syntax:
ConfigurationSettings.GetConfig("MyAppSettings")("connString")

Showing Answers 1 - 1 of 1 Answers

samiksc

  • Jan 17th, 2006
 

Web.config is the asp.net configuration file which stores configuration settings of the folder in which it resides and its subfolders. If a subfolder contains web.config then it overrides the settings of the parent web.config.

In this file you may keep settings for users and their access rights, also changeable parameters like connection strings.

The advantage of keeping such information in web.config is that you don't need to recompile and redeploy the application if any of these settings have to be changed.

ASP.Net runtime automatically detects the changes and recalculates the web to effect changes.

This file is extensible. You can add your own settings to implement your configuration logic. This is possible since it is implemented as an XML file.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions