Asp. NET configuration file hierarchy and inheritance

Source: Internet
Author: User
Tags config configuration settings inheritance parent directory

You can distribute the ASP.net configuration file throughout the application directory to configure the ASP.net application in an inheritance hierarchy. With this structure, you can implement configuration details at the appropriate directory level for the level of application required, without affecting configuration settings in higher directory levels.

Configuration structure

asp.net configuration files, called Web.config files, can appear in multiple directories in a asp.net application. The ASP.net configuration hierarchy has the following characteristics:

Use a configuration file that applies to resources in the directory where the configuration file resides and all of its subdirectories.

Allows you to place the configuration data where it will have the appropriate scope (the entire computer, all WEB applications, a single application, or a subdirectory in the application).

Allows you to override configuration settings inherited from higher levels in the configuration hierarchy. Also allows you to lock configuration settings to prevent them from being overridden by lower-level configuration settings.

Organize the logical groups of configuration settings into sections.

Configure inheritance

All. NET Framework applications inherit basic configuration settings and default values from a file named \microsoft. Net\framework\ Version number \config\machine.config. The Machine.config file is used for server-level configuration settings. Some of these settings cannot be overridden in a configuration file that is located at a lower level in the hierarchy.

. NET client applications (consoles and Windows applications) override inherited settings by using a configuration file named Applicationname.config. The ASP.net application overrides the inherited settings using a configuration file named Web.config.

The root of the ASP.net configuration hierarchy is a file called the root Web.config file, which is located in the same directory as the Machine.config file. The root Web.config file inherits all the settings in the Machine.config file. The root Web.config file includes settings that apply to all ASP.net applications running a specific version of the. NET Framework. Because each asp.net application inherits the default configuration settings from the root Web.config file, you only need to create the Web.config file for the settings that override the default settings.

Inheritance in a collection element

Some configuration elements are collections, such as namespaces elements and customErrors elements.

In a collection, you typically add configuration settings to the collection through the Add child element, remove the configuration settings by using the Remove child element key name, or you can clear the entire collection through the purge child element. Unless duplicates are allowed, the settings that are added in the child configuration file override the same key name settings in the parent configuration file.

Attention

Some collections that exist in earlier versions of the. NET Framework use different element names for the add child element. For example, the CustomErrors element uses the error child element to add a custom error to the collection.

If you receive a request for a file that does not exist in the SubDir1 directory, ASP.net begins the search configuration hierarchy and starts with the most local Web.config file (if it exists, it may be in the current directory, or it may be in the parent directory of the current directory). ASP.net will search for the customErrors error element (asp.net set Schema) element of the StatusCode property equal to "404". Once asp.net finds 404 errors in the configuration settings, returns the URL in the redirect property as a response.

Scope of configuration settings

Configuration settings have different scopes-some have global scope, and some are only valid for application scopes (root Web.config files or machine.config files).

The scope of the configuration section is defined for all sections contained in the ASP.net in the Allowdefinition property of the section element (General Settings Schema) element of the configsections in the Machine.config file. For example, the authentication element (asp.net setting Schema) element has the allowdefinition attribute of the machinetoapplication element. This means that the authentication element can be set at the application-level Machine.config file, the root Web.config file, and the Web.config file. If it is set at the subdirectory level, an error is raised. If no allowdefinition attribute is defined for a section, the default value is everywhere.

The configuration settings for each element in the ASP.net configuration settings and general configuration settings (asp.net) are listed next to the configurable locations in the element information table.

The following table lists the levels of each file in the configuration hierarchy, the name of each file, and a description of the important inheritance characteristics for each file.

configuration level file name file description
server machine.config machine.config file contains the ASP.net schema for all WEB applications on the server. This file is located at the top level of the configuration merge hierarchy.
root Web web.config
Web web.config
asp. NET application root directory weB.config asp.net file for a particular Web.config application is located in the root directory of the application that contains the application to the Web application and inherits down to its branch. The settings for all subdirectories.
Application subdirectory contains settings that apply to this subdirectory and inherit down to all subdirectories in its branch.
client application directory applicat The IonName.configApplicationName.config file contains settings for Windows client applications, not WEB applications.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.