Avoid web.config inheritance in child web application using inheritInChildApplications

200,630

Solution 1

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".

Solution 2

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>

Solution 3

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>

Solution 4

We were getting an error related to this after a recent release of code to one of our development environments. We have an application that is a child of another application. This relationship has been working fine for YEARS until yesterday.

The problem:
We were getting a yellow stack trace error due to duplicate keys being entered. This is because both the web.config for the child and parent applications had this key. But this existed for many years like this without change. Why all of sudden its an issue now?

The solution:
The reason this was never a problem is because the keys AND values were always the same. Yesterday we updated our SQL connection strings to include the Application Name in the connection string. This made the string unique and all of sudden started to fail.

Without doing any research on the exact reason for this, I have to assume that when the child application inherits the parents web.config values, it ignores identical key/value pairs.

We were able to solve it by wrapping the connection string like this

    <location path="." inheritInChildApplications="false">
        <connectionStrings>
            <!-- Updated connection strings go here -->
        </connectionStrings>
    </location>

Edit: I forgot to mention that I added this in the PARENTS web.config. I didn't have to modify the child's web.config.

Thanks for everyones help on this, saved our butts.

Solution 5

If (as I understand) you're trying to completely block inheritance in the web config of your child application, I suggest you to avoid using the tag in web.config. Instead create a new apppool and edit the applicationHost.config file (located in %WINDIR%\System32\inetsrv\Config and %WINDIR%\SysWOW64\inetsrv\config). You just have to find the entry for your apppool and add the attribute enableConfigurationOverride="false" like in the following example:

<add name="MyAppPool" autoStart="true" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated" enableConfigurationOverride="false">
    <processModel identityType="NetworkService" />
</add>

This will avoid configuration inheritance in the applications served by MyAppPool.

Matteo

Share:
200,630

Related videos on Youtube

Blankman
Author by

Blankman

... .. . blank

Updated on June 07, 2020

Comments

  • Blankman
    Blankman almost 4 years

    I am trying to add

    <location inheritInChildApplications="false">
    

    to my parent web application's web.config but it doesn't seem to be working.

    My parent's web.config has:

    <configuration>
        <configSections>
        </configSections>
    
        // 10 or so custom config sections like log4net, hibernate,
    
        <connectionStrings>
        </connectionStrings>
    
        <appSettings>
        </appSettings>
    
        <system.diagnostics>
        </system.diagnostics>
    
        <system.web>
             <webParts>
             </webParts>
             <membership>
             </membership>
    
             <compilation>
             </compilation>
        </system.web>
    
        <location ..>
        <system.web>
            </system.web>
        </location>
    
        <system.webServer>
        </system.webServer>
    

    My child web application is setup as an application in IIS, and is inheriting from the parent's web.config which is causing problems.

    Where exactly should I place the

    <location inheritInChildApplications="false">
    

    so it ignores all the various web.config settings?

  • Blankman
    Blankman about 15 years
    I get this error "The configuration section 'configSections' cannot be read because it is missing a section declaration " in my parents web.config file.
  • Andrew Hare
    Andrew Hare about 15 years
    Can you post your config with the <location> element in it? I would also check out my edit and see if <clear/> might be a better approach for what you are trying to do.
  • PositiveGuy
    PositiveGuy over 14 years
    it doesn't work when you put it right under <configuration>. You can wrap lets say <system.web> node but you can't just put it in the root like this.
  • PositiveGuy
    PositiveGuy over 14 years
    If you put it under <configuration> as the 2nd node in, you get "inheritInChildApplications attribute is not declared". So it's not a valid attribute at that level in the web.config. So how can you say this worked?
  • Adrian Grigore
    Adrian Grigore over 13 years
    -1: I can also confirm that using the location element as shown above does NOT work.
  • Lefteris Livanos
    Lefteris Livanos almost 13 years
    I'm having an issue in that I tried to wrap an <httpdHandlers> section and got the error that I can only put the location in a configuration section. I'm trying to eliminate some custom handlers from passing to my child app as it's causing errors. Can this be done this way?
  • Ryan
    Ryan over 11 years
  • Josh
    Josh over 11 years
    The problem with this answer is that there is <configSections> cannot inside a <location> directive. The official Microsoft writeup on this issue is here: asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc2567701‌​49 The only solution, as far as I can tell is to remove the "duplicate" configSections entries from the application web.config, and then either 1) and hope you don't need them, or 2) upgrade the parent app to ASP.NET 4.0 (so it gains access to the root ASP.NET 4.0 web.config's configSections, which already contains the "duplicate" items you just removed)
  • nabeelfarid
    nabeelfarid about 11 years
    Is it possible to do it the other way around? I find it strange that I have to update the parent, when it is the child who decides if the settings should be inherited or not.
  • aruno
    aruno about 11 years
    @nabeelfarid - I completely agree. If you have a wordpress blog inside a .NET application with a complex web.config it can be a huge pain dealing with clearing it out or preventing inheritance. I think the whole 'location' system is designed more around security for shared hosts that for the compatibility issues most people are finding themselves here for
  • Doctor Jones
    Doctor Jones over 10 years
    @AndrewHare should edit and remove the first half of the answer as it's clearly incorrect.
  • superachu
    superachu about 10 years
    This doesn't work for me? Any thoughts? I've a wcf service which has parent config set to SIT database connection. I have another folder in the same service which says "QA" and it contains same WCF service files as in SIT including the web.config but pointing the database to QA. When I call the wcf service inside the "QA" folder, it takes the connection from parent config only (even I give <location> tag). Please let me know what would be the problem.
  • workabyte
    workabyte about 10 years
    having location as the second node wrapped around all other settings works fine for me!
  • aruno
    aruno over 9 years
    MSDN says 'When false, all settings in Web.config files will be ignored for this application pool' and that just doesn't seem like what you think it means. I'd LOVE this to be the right answer but I just can't get it to work.It almost looks to me like this setting means 'completely disallow a local web.config for this apppool'
  • Matteo Sganzetta
    Matteo Sganzetta over 9 years
    So basically the applications under this app pool are supposed to work without a web.config file? I understand that the "ignored web.config" is the one in the root folder. I used it few times successfully. Make sure the child application doesn't depend on configurations in the root web.config (try to run the child app in a separate root folder).
  • Matteo Sganzetta
    Matteo Sganzetta over 9 years
    You can also check the method #2 on this page, although I haven't tested it iislogs.com/steveschofield/2009/09/20/…
  • aruno
    aruno over 9 years
    my child application is actually an exact copy of the parent application. I want to be able to put /preview so people can test a new version before making it live. Everybody always suggests <location> for fixing this issue so I was very excited to read your post. However it complains The entry 'default' has already been added. for an AppFabric related config entry even when I use enableConfigurationOverride="false"
  • aruno
    aruno over 9 years
    also if I set enableConfigurationOverride="false" on my root application it completely kills the root application and it won't even work :-(
  • Web User
    Web User over 8 years
    @NickCecil how do I achieve this in IIS 6? inheritInChildApplications is not being accepted as a valid parameter for the <location /> element. My website is running SharePoint (2007). I created an application in a virtual directory under this website, managed by its own application pool. Yet, I am encountering conflicts between SharePoint's configuration and this application. See this question I posted in Server Fault.
  • Francis Ducharme
    Francis Ducharme over 7 years
    My application that I created as a child of a web site still wants to load the DLLs from the parent website. Apparently, I can't use <location> for runtime...
  • Kiquenet
    Kiquenet over 7 years
    I have custom sections
  • Mohamed Nuur
    Mohamed Nuur about 7 years
    This solved my issue. I had a web app with EF6.1.3 and child web app with EF5. Upgrading the child web app was out of the question, so I had to use this technique to make both work and it worked. I followed this example, changing myNotInheritedSections to ef6Private and unwantedSection is the entityFramework section.
  • Si8
    Si8 about 7 years
    Should I wrap everything in the child site Web.config?
  • asteriskdothmg
    asteriskdothmg almost 6 years
    Can you help why mine's not working, Here's my code <configSections> <sectionGroup name="ef6Private"> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFramework‌​Section, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <location path="." inheritInChildApplications="false"> <ef6Private> <entityFramework /> </ef6Private> </location>
  • SteveP
    SteveP over 3 years
    Hi Andrew, thanks for the tip using <clear /> in the child config, worked great for using a different set of rewrite rules in a sub directory.
  • madoxdev
    madoxdev over 2 years
    As a comment here - the solution doesn't work for <dependent assemblies> under <runtime> section. Server throws 500 straight away...