Wednesday, April 15, 2009

Web.config Hierarchy in ASP.NET

ASP.NET integrates the settings in configuration files (the Machine.config and Web.config files) into a single inheritance hierarchy. With a few exceptions, you can place a Web.config file wherever you need to override the configuration settings that are inherited from a configuration file located at a higher level in the hierarchy.

We can have web config file at each folder or sub folder of the web application which overrides the higher level config.

System wide configuration settings are defined in the Machine.config for the .NET Framework. The Machine.config file is located in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG folder. By including Web.config files in sub-folders, we can override the settings defined in the Web.config file in the application's root folder.

The following are sample section declarations from a Machine.config file:

<<section name="processModel"
type="System.Web.Configuration.ProcessModelSection, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
allowdefinition="MachineOnly" allowlocation="false"/>>

allowDefnition attribute and it's significance in machine config file.
  • If allowDefinition is MachineOnly , then we can not override this section either in application level or in folder level. The only section declared in the Machine.config file with this settings is processModel. If you are trying to override the configuration in web.config file, then "Parser Error Message: It is an error to use a section registered as allowDefinition='MachineOnly' beyond machine.config" error is thrown.
  • If allowDefinition is MachineToApplication, then we can override these sections by the root directory Web.config. Sections with this setting in Machine.config are authentication, machineKey, sessionState, trust, and securityPolicy.
  • If allowDefinition attribute is omitted in a section declaration of the Machine.config file, we can override that section at any level.