Microsoft KB Archive/815174

= How To Make Application and Directory-Specific Configuration Settings in an ASP.NET Application =

Article ID: 815174

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 1.1

-



IN THIS TASK

 * SUMMARY
 * Use the Location Element in the Machine.config File
 * Use the Location Element in the Web.config File
 * REFERENCES



SUMMARY
This step-by-step article describes how to use ASP.NET to make application-specific and directory-specific configuration settings. The Web.config file resides in the root directory of an ASP.NET application. The Web.config file specifies configuration information that is specific to the application. You can also set configuration settings on a per-directory basis. There are two ways to do so:
 * Distribute multiple Web.config files to directories in the application. Add a Web.config file in the ASP.NET application directory with settings that override settings in a higher-level Web.config file or in the system machine configuration (Machine.config) file.
 * Add per-directory or per-file settings directly to the application Web.config file. This method uses a single Web.config file to turn on per-directory configuration settings. You can also use this method in the Machine.config file to force configuration settings on ASP.NET Web applications and then to stop Web.config files from overriding Machine.config settings.

back to the top

Use the Location Element in the Machine.config File
To specify settings that apply to a Web application or directory, you can add the  element to the  element of a system Machine.config file. This is useful when you centralize configuration settings in a single file. This is also useful in Web-hosting environments to mandate specific configuration settings on individual Web applications.

The   element contains two attributes, path and allowOverride. The path attribute defines the site or virtual directory that the configuration settings cover. To specify that the settings in the   element apply to the default Web site, set the path attribute to Default Web Site. To specify that the settings apply to the application that is named MyApp in the default Web site, set the path attribute to &quot;Default Web Site/MyApp&quot;.

When the allowOverride attribute is false, the Web.config files in the Web application directories cannot override the settings that you specified in the   element. This is a useful setting in environments where you must restrict application developers in how they configure a Web application. The following example shows a part of a Machine.config file that requires authentication to access the MyApp application on the default Web site and that cannot be overridden by settings in a Web.config file:    

back to the top

Use the Location Element in the Web.config File
To specify settings that apply to a specific application or directory, add the  element to the  element of an application Web.config file. The  element typically contains a  element and other configuration elements exactly as you use them in the Web.config file. The path attribute of the  element specifies the virtual directory or the file name where the location configuration items apply. The following example shows part of an application Web.config file that specifies custom error messages for the forum virtual directory.    <error statusCode=&quot;404&quot; redirect=&quot;forum-file-not-found.aspx&quot; /> </customErrors> </system.web>

back to the top

<div class="references_section">