Knowledge Essentials - 3Essentials Hosting

Web.config inheritence

Article ID: 610

 Back to Search

A frequently misunderstood aspect of ASP.net is the inheritence aspect of the web.config.  Many new ASP.net developers are under the impression that if they set a subfolder to be an IIS application, that it will only use the web.config found there, and completely ignore the root application's (the main website) web.config completely.  Not correct... unless you specifically tell it to.  Read on...

Scott Forsyth's Blog gives a nice discussion, and the following is a brief excerpt:
 

"Understanding which ASP.NET files and folders are inherited through folders and applications makes development and troubleshooting so much easier.

Here is the summary to keep in mind:

  • web.config files inherit all the way down the tree, past all application boundaries.
  • global.asax only lives within its application
  • /bin and the /app_{folders} only live within their application

So, this means that anything set in the root web.config file will inherit down the entire site, even if some folders are marked as applications. 

Where this gets messy is if the web.config file has references to assemblies but sub-applications don't have those assemblies.  For example, let's say that you have a HTTP Module configured in the root of the site and referenced from the site web.config file.  If you have a sub-application called /subfolder which is marked as an application, then /subfolder will attempt to load the HTTP Handler from /subfolder/bin.  Since it doesn't exist, an error will be thrown. "

Brendan Kowitz's .NET blog offers a solution for disabling the inheritence with .NET 2.0 applications, and quoted here, shown adding this attribute to the web.config of your root app's web.config:

You can read more about how config files get inherited on msdn but here's a tip for stopping settings in the root app from getting inherited. The <location> tag is the only tag I've come across which has the inheritInChildApplications attribute. So to target the main <system.web> just wrap it in the location tag as seen below. 

  1. <location path="." inheritInChildApplications="false">  
  2. <system.web>  
  3.  ...  
  4. </system.web>  
  5. </location> 

If you are on IIS 7 you will need to enclose the system.webserver section also.

Some configuration elements are collections such as the namespaces element and the customErrors element.

In collections, configuration settings are typically added to the collection via an add child element, removed by key name via a remove child element, or the entire collection can be cleared via a clear child element. An added setting in a child configuration file overrides a setting of the same key name in a parent configuration file unless duplicates are allowed.

 

1. Remove the offending assembly in the system.web section of the child application's web.config file.


<compilation>
    <assemblies>
        <remove assembly="MyUnrequiredAssembly" />
    </assemblies>
</compilation>

If you can't seem to get rid of the offending assembly, check for httpModules:


<httpModules>
    <remove name="MyUnrequiredAssembly" />
</httpModules>

Sometimes, it is easier to start the child application with a clean slate. In that case:

2. Clear all the assemblies in the system.web section of the child application's web.config file.


<compilation>
    <assemblies>
        <clear />
        <!-- Add required assemblies here -->
    </assemblies>
</compilation>

 

 
Downloads Associated With This Article
No downloads are currently associated with this article.