The ASP.NET Configuration Model

ASP.NET as well as .NET applications have one more excellent feature, ASP.NET uses a flexible configuration management system that keeps the application configuration separate from application code. ASP.NET configuration is stored in XML files with the .config extension, which makes it easy for users to change it before, during and after deployment.

The configuration file we all are aware of is the web.config file found in the application root, which when added has almost very simple configurations. But guess what that is not the only configuration file that your ASP.NET application relies on, there are other config files that your application inherit that configuration by default, next I will show the Configuration Hierarchy in ASP.NET. [more]

Configuration Hierarchy

  1. Server settings – machine.config : This is at the root of the configuration hierarchy. It defines global, default settings for all of the applications on the(a) server. The machine.config is located in the Config folder for the version of the .NET Framework installed on the computer. In default installation will be found in this path C:WindowsMicrosoft.NETFrameworkv2.0.50727CONFIGmachine.config *.
  2. Root Web setting – web.config : This defines the default settings for ASP.NET applications on the(a) server, which inherits all the settings from machine.config. Can be located at C:WindowsMicrosoft.NETFrameworkv2.0.50727CONFIGweb.config*.
  3. Web site settings – web.config : This file is the first optional file in the hierarchy it defines some settings that will be applied on all the ASP.NET applications within a website. For example, to set some default settings for all web applications in the Default Web Site that comes in the IIS, place a web.config file in this path C:inetpubwwwrootweb.config *.
  4. ASP.NET application root settings – web.config : This comes to be the web.config we all know that is found in the application root.This file is also optional.
  5. ASP.NET application subfolder settings – web.config : This file is found in subfolders of an application to set some specific settings on the pages contained in this folder. For Example, Admin folder needs an extra <authorization> setting to not allow non admins for accessing the folder.

NOTE : Although some of the settings in the machine.config file can be overridden by settings in the web.config file, other settings that apply to ASP.NET as a whole are protected and cannot be overridden.

Merging configuration at runtime

At runtime, all these settings are merged, all configuration settings are retrieved from the machine.config, then settings from the default web.config are then retrieved, new settings are add to those of the machine.config, changed settings override the orignal ones. And So on, for the rest of the config files.

Editing the web.config

It is highly recommended not to change the default settings in the machine.config and in the default web.config of a server, unless you know exactly what you are doing.
Example for changing, there is a default connectionString in the machine.config named LocalSqlServer, a lot of extensions and providers strongly rely on it, but this default connection string uses sqlexpress which is not preferred by a lot of developers which do not want to override it in every application they use, so you can change that.

Tools for editing Web.Config; Since the web.config is XML based there are many tools that can help reading and writing settings in it, like notepad , also there are tools that are made for the sake of editing the web.config. Example tools:

  1. Web Administration Tool : which enables easy and quick creation and editing web.config files for specific web application and subfolders. The web administration tool comes with Visual Studio 2005, 2008, It is found in the Project/WebSite menu usually the last item named
  2. My friend here found another tool that helps changing web.config file in a nice visual tree.

* These paths are the default when installing the visual studio







Leave a Reply

Your email address will not be published. Required fields are marked *