Avoid web.config inheritance in child web application using inheritInChildApplications Avoid web.config inheritance in child web application using inheritInChildApplications asp.net asp.net

Avoid web.config inheritance in child web application using inheritInChildApplications


As the commenters for the previous answer mentioned, you cannot simply add the line...

<location path="." inheritInChildApplications="false">

...just below <configuration>. Instead, you need to wrap the individual web.config sections for which you want to disable inheritance. For example:

<!-- disable inheritance for the connectionStrings section --><location path="." inheritInChildApplications="false">   <connectionStrings>   </connectionStrings></location><!-- leave inheritance enabled for appSettings --><appSettings></appSettings><!-- disable inheritance for the system.web section --><location path="." inheritInChildApplications="false">   <system.web>        <webParts>        </webParts>        <membership>        </membership>        <compilation>        </compilation>      </system.web> </location>

While <clear /> may work for some configuration sections, there are some that instead require a <remove name="..."> directive, and still others don't seem to support either. In these situations, it's probably appropriate to set inheritInChildApplications="false".


It needs to go directly under the root <configuration> node and you need to set a path like this:

<?xml version="1.0"?><configuration>    <location path="." inheritInChildApplications="false">         <!-- Stuff that shouldn't be inherited goes in here -->    </location></configuration>

A better way to handle configuration inheritance is to use a <clear/> in the child config wherever you don't want to inherit. So if you didn't want to inherit the parent config's connection strings you would do something like this:

<?xml version="1.0"?><configuration>    <connectionStrings>        <clear/>        <!-- Child config's connection strings -->    </connectionStrings></configuration>


I put everything into:

<location path="." inheritInChildApplications="false">....</location>

except: <configSections/>, <connectionStrings/> and <runtime/>.

There are some cases when we don't want to inherit some secions from <configSections />, but we can't put <section/> tag into <location/>, so we have to create a <secionGroup /> and put our unwanted sections into that group. Section groups can be later inserted into a location tag.

So we have to change this:

<configSections>  <section name="unwantedSection" /></configSections>

Into:

<configSections>  <sectionGroup name="myNotInheritedSections">    <section name="unwantedSection" />  </sectionGroup></configSections><location path="." inheritInChildApplications="false">    <myNotInheritedSections>        <unwantedSection />    </myNotInheritedSections></location>