Configuration File FormatASP.NET configuration files are XML-based text files--each named web.config--that can appear in any directory on an ASP.NET Web application server. Each web.config file applies configuration settings to the directory it is located in and to all virtual child directories beneath it. Settings in child directories can optionally override or modify settings specified in parent directories. The root configuration file--WinNT\Microsoft.NET\Framework\<version>\config\machine.config--provides default configuration settings for the entire machine. ASP.NET configures IIS to prevent direct browser access to web.config files to ensure that their values cannot become public (attempts to access them will cause ASP.NET to return 403: Access Forbidden).At run time ASP.NET uses these web.config configuration files to hierarchically compute a unique collection of settings for each incoming URL target request (these settings are calculated only once and then cached across subsequent requests; ASP.NET automatically watches for file changes and will invalidate the cache if any of the configuration files change). For example, the configuration settings for the URL http://myserver/myapplication/mydir/page.aspx would be computed by applying web.config file settings in the following order:
Base configuration settings for machine. C:\WinNT\Microsoft.NET\Framework\v.1.00\config\machine.config Overridden by the configuration settings for the site (or the root application). C:\inetpub\wwwroot\web.config Overridden by application configuration settings. D:\MyApplication\web.config Overridden by subdirectory configuration settings. D:\MyApplication\MyDir\web.config Configuration Section Handlers and SectionsA web.config file is an XML-based text file that can contain standard XML document elements, including well-formed tags, comments, text, cdata, and so on. The file may be ANSI, UTF-8, or Unicode; the system automatically detects the encoding. The root element of a web.config file is always a <configuration> tag. ASP.NET and end-user settings are then encapsulated within the tag, as follows:
<configuration>
<!- Configuration settings would go here. -->
</configuration>
<configuration>
<configSections>
<sectionGroup name="system.web">
<section
name="httpModules"
type="System.Web.Configuration.HttpModulesConfigurationHandler,System.Web"
/>
</sectionGroup>
</configSections>
<system.web>
<httpModules>
<add
name="CookielessSession"
type="System.Web.SessionState.CookielessSessionModule,System.Web"
/>
<add
name="OutputCache"
type="System.Web.Caching.OutputCacheModule,System.Web"
/>
<add
name="Session"
type="System.Web.SessionState.SessionStateModule,System.Web"
/>
<add
name="WindowsAuthentication"
type="System.Web.Security.WindowsAuthenticationModule,System.Web"
/>
<add
name="FormsAuthentication"
type="System.Web.Security.FormsAuthenticationModule,System.Web"
/>
<add
name="PassportAuthentication"
type="System.Web.Security.PassportAuthenticationModule,System.Web"
/>
<add
name="UrlAuthorization"
type="System.Web.Security.UrlAuthorizationModule,System.Web"
/>
<add
name="FileAuthorization"
type="System.Web.Security.FileAuthorizationModule,System.Web"
/>
</httpModules>
</system.web>
</configuration>
Using Location and PathBy default, all configuration settings defined within the top-level <configuration> tag are applied to the current directory location of the containing web.config file and to all of the child paths beneath it. You can optionally apply configuration settings to specific child paths under the current config file by using the <location> tag with an appropriately constraining path attribute. If the config file is the main machine.config file, you can apply settings to specific virtual directories or applications. If the config file is a web.config file, you can apply settings to a specific file, child directory, virtual directory, or application.
<configuration>
<location path="EnglishPages">
<system.web>
<globalization
requestEncoding="iso-8859-1"
responseEncoding="iso-8859-1"
/>
</system.web>
</location>
<location path="EnglishPages/OneJapanesePage.aspx">
<system.web>
<globalization
requestEncoding="Shift-JIS"
responseEncoding="Shift-JIS"
/>
</system.web>
</location>
</configuration>
Locking down configuration settingsIn addition to specifying path information using the <location> tag, you can also specify security so that settings cannot be overridden by another configuration file further down the configuration hierarchy. To lock down a group of settings, you can specify an allowOverride attribute on the surrounding <location> tag and set it to false. The following code locks down impersonation settings for two different applications.
<configuration>
<location path="app1" allowOverride="false">
<system.web>
<identity impersonate="false" userName="app1" password="app1pw" />
</system.web>
</location>
<location path="app2" allowOverride="false">
<system.web>
<identity impersonate="false" userName="app2" password="app2pw" />
</system.web>
</location>
</configuration>
Note that if a user tries to override these settings in another configuration file, the configuration system will throw an error:
<configuration>
<system.web>
<identity userName="developer" password="loginpw" />
</system.web>
</configuration>
Standard ASP.NET Configuration SectionASP.NET ships with a number of standard configuration section handlers that are used to process configuration settings within web.config files. The following table provides brief descriptions of the sections, along with pointers to more information.
|