40

I have a content management application in the root of my website, and I'm trying to use a different app (a billing application) under a sub-folder. Unfortunately, the web.config of the root site is interfering with the sub-app.

Is there a way to just disable web.config inheritance for a sub-folder?

Update: As linked by Stephen Burris, using the <location> tag can prevent inheritance for part of the web config, as follows:

<?xml version="1.0"?>
<configuration>
<configSections>
    ....
</configSections>
<location path="." inheritInChildApplications="false">
    <appSettings>
        ....
    </appSettings>
    <connectionStrings/>
    <system.web>
        ....
    </system.web>
    <system.codedom>
        ....
    </system.codedom>
    <system.webServer>
        ....
    </system.webServer>
</location>
<runtime>
    ....
</runtime>
</configuration>

The <configSections> and <runtime> sections will not accept being enclosed in the tag...so I guess this only does most of the job. Anybody know how to do it better?

Community
  • 1
  • 1
JoshRivers
  • 9,920
  • 8
  • 39
  • 39
  • 1
    **tldr, don't bother; from 11+ years in the future.** I've in the process of moving applications to *distinct sites* to avoid this sort of nonsense. There is not a clean way to deal with complex inheritance for many reasons: 1) can't disable inheritance of certain sections at all; 2) can't selectively disable inheritance of portions of other sections; 3) suppressing inheritance (where it works) inconsistently blocks inheriting from the grandparent (eg. host config) as well. – user2864740 Feb 10 '20 at 22:02

5 Answers5

22

There is an attribute that you can use in the root web.config file to cause it not to have its contents become inherited by child applications.

inheritInChildApplications

Blog about inheritInChildApplications

MSDN article on ASP.NET Configuration File Hierarcy and Inheritance

Put the part of the config that is not for inheritance inside

<location inheritInChildApplications="false">
     <NotInheritedConfigPart/>
</location>

Config sections seem to be impossible to not inherit, but other parts of configuration can be "commented" out like this and don't get inherited.

Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93
SBurris
  • 7,378
  • 5
  • 28
  • 36
  • I could not use "inheritInChildApplications" because VS didn't like it. What I did instead is negate the things IIS was complaining about by using a tag. That worked for me. – dyslexicanaboko Nov 09 '12 at 23:13
  • 5
    There is an issue with this approach. You can disable inheritance of items in < system.web /> but this does nothing to the < configSections /> part. Anybody know how to disable inheritance of items in < configSections />? – Ropstah Apr 22 '09 at 12:03
20

If you can use 2 separate application pools, you can completely stop inheritance by using an attibute enableConfigurationOverride="false" in the applicationHost.config file as I described in this question: “Entry has already been added” - Two Separate App Pools

<add name="MyAppPool" enableConfigurationOverride="false" autoStart="true" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated" >
    <processModel identityType="NetworkService" />
</add>
Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93
Matteo Sganzetta
  • 768
  • 7
  • 20
  • No UI settings? Listed in https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/applicationpools/add/ – user2864740 Feb 10 '20 at 22:12
  • 3
    Was not able to use this approach in IIS 10, even when converting a top-level folder to a *new application* with a distinct application pool. The settings of the sub-application where still inherited from the main added. – user2864740 Feb 10 '20 at 22:26
10

In my opinion every time I've struggled with this the answer ends up being effectively NO - and I'm leaving this here for my future self to find so he doesn't waste any more time on it.

I've found this to be a huge problem when you just want to add something as a virtual directory inside an existing site. With a complex web.config files I've always just ended up giving up and moving it to a different application altogether.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
7

I would explicitly define all of the settings required - never assume that any setting is still set to the default value.

For example, if you're defining a connectionString include a <clear /> tag before the <add name=... />, etc. For Membership define all of the attributes, including the cookie name. And so on.

It may make the file a bit bigger but it will definitely help you avoid the "but it worked on my box" scenario too :-)

devstuff
  • 8,277
  • 1
  • 27
  • 33
0

I have faced this situation a few times. It seems that the parent poster's instincts are in the ball park. I have found that adding two entries seem to have solved it—both a <location> entry.

  1. Find your <system.web></system.web>. Wrap it with a <location path="." inheritInChildApplications="false">.

    Now check the parent site and make sure it still works. Check the child: does it work or give an error?

  2. If you still have an error, do the same thing with <system.webserver>; wrap the same <location> tag around it.

In summary - in the PARENT:

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

and maybe:

<location path="." inheritInChildApplications="false">
  <system.webserver>
    ...
  </system.webserver>
</location>
Michael
  • 8,362
  • 6
  • 61
  • 88
Michael
  • 173
  • 1
  • 3