Udostępnij za pośrednictwem


How to: Configure Specific Directories Using Location Settings

By using the location element with an appropriate value for the path attribute, you can apply configuration settings to specific folders and files. The path attribute can be used to identify a specific file or child directory to which unique configuration settings apply. Only one file path can be used in the path attribute. The location element takes precedence over any ASP.NET configuration setting in the current Web.config file.

You can set the location element programmatically by using the ASP.NET configuration API, or you can set it by using the Locations tab in the ASP.NET MMC snap-in.

The default value for the Path property is ".". Multiple location elements can exist in the same configuration file, specifying different scopes for the same configuration sections, as in the following example:

  <location path="." >
    <section1 … />
    <section2 … />
  </location>
  <location path="Default Web Site" >
    <section1 … />
    <section2 … />
  </location
  <location path="Default Web Site/MyApplication/Admin/xyz.html" >
    <section1 … />
    <section2 … />
  </location>

Example

The following code example shows a configuration file that specifies settings for two specific resources:

  • Settings that apply only to the Sub1 child directory are between the <location> and </location> tags, with a path attribute set to Sub1.

  • Settings that apply only to the Sub2 child directory are between the <location> and </location> tags, with a path attribute set to Sub2.

Settings that apply to the current directory and all child directories are between the <configuration> and </configuration> tags, but they are not enclosed by the <location> and </location> tags.

<configuration>
  <system.web>
    <sessionState cookieless="true" timeout="10" />
  </system.web>

  <!-- Configuration for the "Sub1" subdirectory. -->
  <location path="sub1">
    <system.web>
      <httpHandlers>
        <add verb="*" path="Sub1.Scott" type="Sub1.Scott" />
        <add verb="*" path="Sub1.David" type="Sub1.David" />
      </httpHandlers>
    </system.web>
  </location>

  <!-- Configuration for the "Sub2" subdirectory. -->
  <location path="sub2">
    <system.web>
      <httpHandlers>
        <add verb="*" path="Sub2.Scott" type="Sub2.Scott" />
        <add verb="*" path="Sub2.David" type="Sub2.David" />
      </httpHandlers>
    </system.web>
  </location>
</configuration>

See Also

Tasks

How to: Lock ASP.NET Configuration Settings

Other Resources

MMC Snap-In for ASP.NET