169

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

7 Answers7

215

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

David Sherret
  • 101,669
  • 28
  • 188
  • 178
Nick Cecil
  • 2,890
  • 2
  • 19
  • 10
  • 13
    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. – nabeelfarid May 09 '13 at 13:43
  • @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 – Simon_Weaver May 14 '13 at 22:25
  • 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 tag). Please let me know what would be the problem. – superachu Apr 30 '14 at 20:56
  • @NickCecil how do I achieve this in IIS 6? `inheritInChildApplications` is not being accepted as a valid parameter for the `` 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](http://serverfault.com/questions/727228/virtual-directory-application-web-config-conflicting-with-sharepoint-web-config) I posted in Server Fault. – Web User Oct 07 '15 at 07:34
  • 2
    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 `` for runtime... – Francis Ducharme Sep 16 '16 at 16:05
  • Should I wrap everything in the child site Web.config? – Si8 May 10 '17 at 13:23
  • As a comment here - the solution doesn't work for under section. Server throws 500 straight away... – madoxdev Dec 23 '21 at 09:41
68

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>
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 18
    I get this error "The configuration section 'configSections' cannot be read because it is missing a section declaration " in my parents web.config file. – Blankman Apr 23 '09 at 15:37
  • Can you post your config with the element in it? I would also check out my edit and see if might be a better approach for what you are trying to do. – Andrew Hare Apr 23 '09 at 15:49
  • 6
    it doesn't work when you put it right under . You can wrap lets say node but you can't just put it in the root like this. – PositiveGuy Oct 15 '09 at 13:07
  • If you put it under 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? – PositiveGuy Oct 15 '09 at 13:12
  • 12
    -1: I can also confirm that using the location element as shown above does NOT work. – Adrian Grigore Nov 26 '10 at 12:34
  • I'm having an issue in that I tried to wrap an `` 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? – Valien Aug 05 '11 at 14:44
  • The problem with this answer is that there is cannot inside a directive. The official Microsoft writeup on this issue is here: http://www.asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc256770149 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) – Josh Jan 08 '13 at 20:34
  • @AndrewHare should edit and remove the first half of the answer as it's clearly incorrect. – Doctor Jones Sep 11 '13 at 15:13
  • having location as the second node wrapped around all other settings works fine for me! – workabyte May 15 '14 at 15:35
  • Hi Andrew, thanks for the tip using in the child config, worked great for using a different set of rewrite rules in a sub directory. – SteveP Sep 23 '20 at 16:06
24

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>
cryss
  • 4,130
  • 1
  • 29
  • 34
  • I have **custom sections** – Kiquenet Oct 07 '16 at 06:53
  • 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. – Mohamed Nuur Mar 29 '17 at 08:19
  • Can you help why mine's not working, Here's my code `
    `
    – asteriskdothmg Jun 28 '18 at 08:52
9

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.

Kenneth Garza
  • 1,886
  • 14
  • 12
6

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

Matteo Sganzetta
  • 768
  • 7
  • 20
  • 1
    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' – Simon_Weaver Jan 27 '15 at 23:52
  • 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 Jan 28 '15 at 14:34
  • 1
    You can also check the method #2 on this page, although I haven't tested it http://iislogs.com/steveschofield/2009/09/20/control-web-config-inheritance-with-iis-7-asp-net-options/ – Matteo Sganzetta Jan 28 '15 at 14:35
  • 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 `` 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"` – Simon_Weaver Jan 28 '15 at 22:46
  • also if I set `enableConfigurationOverride="false"` on my root application it completely kills the root application and it won't even work :-( – Simon_Weaver Jan 29 '15 at 23:39
2

This is microsoft's page on the location tag: http://msdn.microsoft.com/en-us/library/b6x6shw7%28v=vs.100%29.aspx

It may be helpful to some folks.

Zack
  • 2,789
  • 33
  • 60
Mark
  • 1,455
  • 3
  • 28
  • 51
  • 5
    This link is dead. Try this one: http://msdn.microsoft.com/en-us/library/b6x6shw7%28v=vs.100%29.aspx – Ryan Sep 14 '12 at 21:03
1

We're getting errors about duplicate configuration directives on the one of our apps. After investigation it looks like it's because of this issue.

In brief, our root website is ASP.NET 3.5 (which is 2.0 with specific libraries added), and we have a subapplication that is ASP.NET 4.0.

web.config inheritance causes the ASP.NET 4.0 sub-application to inherit the web.config file of the parent ASP.NET 3.5 application.

However, the ASP.NET 4.0 application's global (or "root") web.config, which resides at C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config and C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config (depending on your bitness), already contains these config sections.

The ASP.NET 4.0 app then tries to merge together the root ASP.NET 4.0 web.config, and the parent web.config (the one for an ASP.NET 3.5 app), and runs into duplicates in the node.

The only solution I've been able to find is to remove the config sections from the parent web.config, and then either

  1. Determine that you didn't need them in your root application, or if you do
  2. Upgrade the parent app to ASP.NET 4.0 (so it gains access to the root web.config's configSections)
Josh
  • 7,232
  • 8
  • 48
  • 75