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?

0

7 Answers 7

216

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

7
  • 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. May 9, 2013 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 May 14, 2013 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 <location> tag). Please let me know what would be the problem.
    – superachu
    Apr 30, 2014 at 20:56
  • @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.
    – Web User
    Oct 7, 2015 at 7: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 <location> for runtime... Sep 16, 2016 at 16:05
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>
10
  • 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, 2009 at 15:37
  • 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. Apr 23, 2009 at 15:49
  • 6
    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. Oct 15, 2009 at 13:07
  • 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? Oct 15, 2009 at 13:12
  • 12
    -1: I can also confirm that using the location element as shown above does NOT work. Nov 26, 2010 at 12:34
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>
3
  • I have custom sections
    – Kiquenet
    Oct 7, 2016 at 6: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. Mar 29, 2017 at 8:19
  • 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.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <location path="." inheritInChildApplications="false"> <ef6Private> <entityFramework /> </ef6Private> </location> Jun 28, 2018 at 8: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.

0
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

5
  • 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' Jan 27, 2015 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). Jan 28, 2015 at 14:34
  • 1
    You can also check the method #2 on this page, although I haven't tested it iislogs.com/steveschofield/2009/09/20/… Jan 28, 2015 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 <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" Jan 28, 2015 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 :-( Jan 29, 2015 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.

1
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.