Home
%3CLINGO-SUB%20id%3D%22lingo-sub-297627%22%20slang%3D%22en-US%22%3EHow%20to%20prevent%20web.config%20files%20to%20be%20overwritten%20by%20config%20files%20in%20application%20subfolders%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-297627%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EThe%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln4%22%3E%3CCODE%3Eweb.config%3C%2FCODE%3E%3C%2FSPAN%3E%20%3CSPAN%3Efiles%20in%20application%3C%2FSPAN%3E%20%3CSPAN%3Esubfolders%20can%20override%20the%20settings%20of%20the%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln4%22%3E%20%3CCODE%3Eweb.config%3C%2FCODE%3E%3C%2FSPAN%3E%20%3CSPAN%3Efiles%20in%3C%2FSPAN%3E%20%3CSPAN%3Eparent%20folders.%20In%20order%20to%20prevent%20parent%20config%20files%20to%20be%20overwritten%2C%20you%20need%20to%20configure%20IIS%20to%20ignore%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20files%20in%20application%20subfolders.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAn%20example%20for%20configuration%20hierarchy%20in%20IIS%20is%20below.%20In%20this%20example%2C%20there%20is%20a%20website%20called%20%3CSTRONG%3ESite1%3C%2FSTRONG%3E.%20This%20website%20has%20two%20subfolders%3A%20%3CSTRONG%3ESubDir1%3C%2FSTRONG%3E%20and%20%3CSTRONG%3ESubDir2%3C%2FSTRONG%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F62176i5A30A4A50314E5CC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22IIS-configuration.PNG%22%20title%3D%22IIS-configuration.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%3E%3CEM%3EDiagram%3A%20A%20sample%20IIS%20configuration%20hierarchy%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1704825497%22%20id%3D%22toc-hId-1766522722%22%3EExample%3C%2FH2%3E%0A%3CP%3EIn%20this%20example%2C%20If%20you%20don%E2%80%99t%20want%20the%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20files%20in%20%3CSTRONG%3ESubDir1%3C%2FSTRONG%3E%20and%20%3CSTRONG%3ESubDir2%3C%2FSTRONG%3E%20folders%20to%20override%20settings%20in%20the%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20file%20of%20%3CSTRONG%3ESite1%3C%2FSTRONG%3E%20folder%2C%20use%20%3CSPAN%3E%3CCODE%3EallowSubDirConfig%3C%2FCODE%3E%3C%2FSPAN%3E%20attribute%20in%20your%20%3CCODE%3EapplicationHost.config%3C%2FCODE%3E%20file.%20For%20instance%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%26lt%3Bsites%26gt%3B%0A%20%20%20%26lt%3Bsite%20name%3D%22Site1%22%20id%3D%221%22%26gt%3B%0A%20%20%20%20%20%20%26lt%3Bapplication%20path%3D%22%2F%22%26gt%3B%0A%20%20%20%20%20%20%20%20%20%26lt%3BvirtualDirectory%20path%3D%22%2F%22%20physicalPath%3D%22D%3A%5CWebApps%5CSite1%22%20allowSubDirConfig%3D%22false%22%20%2F%26gt%3B%0A%20%20%20%20%20%20%26lt%3B%2Fapplication%26gt%3B%0A%20%20%20%26lt%3B%2Fsite%26gt%3B%0A%26lt%3B%2Fsites%26gt%3B%26nbsp%3B%3C%2FPRE%3E%0A%3CP%3EFor%20enabling%20this%20setting%20in%20all%20websites%20hosted%20by%20IIS%2C%20use%26nbsp%3B%3CCODE%3EvirtualDirectoryDefaults%3C%2FCODE%3E%20tag%3A%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%26lt%3Bsites%26gt%3B%0A%20%20%20%E2%80%A6%0A%20%20%20%26lt%3BvirtualDirectoryDefaults%20allowSubDirConfig%3D%22false%22%20%2F%26gt%3B%0A%26lt%3B%2Fsites%26gt%3B%3C%2FPRE%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%20%3C%2FSTRONG%3E%3CCODE%3EallowSubDirConfig%3C%2FCODE%3E%20attribute%20can%20be%20used%20only%20in%20%3CSPAN%3EapplicationHost.config%3C%2FSPAN%3E.%20It%20is%20not%20available%20for%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20files.%26nbsp%3B%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH2%20id%3D%22toc-hId--847331464%22%20id%3D%22toc-hId--785634239%22%3EIgnore%20config%20files%20for%20certain%20subfolders%20only%3C%2FH2%3E%0A%3CP%3EYou%20can%20also%20configure%20IIS%20to%20ignore%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20files%20in%20certain%20subfolders%20only.%20However%2C%20these%20folders%20has%20to%20be%20%3CSTRONG%3Evirtual%20folders%3C%2FSTRONG%3E.%20In%20the%20example%20below%2C%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20files%20are%20ignored%20in%20all%20subfolders%20except%20the%20one%20in%20%3CSTRONG%3EsubDir2%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%26lt%3Bsites%26gt%3B%0A%20%20%20%26lt%3Bsite%20name%3D%22Site1%22%20id%3D%221%22%26gt%3B%0A%20%20%20%20%20%20%26lt%3Bapplication%20path%3D%22%2F%22%26gt%3B%0A%20%20%20%20%20%20%26lt%3BvirtualDirectory%20path%3D%22%2F%22%20physicalPath%3D%22D%3A%5CWebApps%5CSite1%22%20allowSubDirConfig%3D%22false%22%20%2F%26gt%3B%0A%20%20%20%20%20%20%26lt%3BvirtualDirectory%20path%3D%22%2FSubDir2%22%20physicalPath%3D%22D%3A%5CWebApps%5CSite1%5CSubDir2%22%20allowSubDirConfig%3D%22true%22%20%2F%26gt%3B%0A%20%20%20%20%20%20%26lt%3B%2Fapplication%26gt%3B%0A%20%20%20%26lt%3B%2Fsite%26gt%3B%0A%26lt%3B%2Fsites%26gt%3B%3C%2FPRE%3E%0A%3CP%3EPlease%20note%20that%20the%20examples%20above%20use%20%3CCODE%3E%3CAPPLICATION%3E%3C%2FAPPLICATION%3E%3C%2FCODE%3E%20%3CCODE%3Epath%3D%22%2F%22%26gt%3B%3C%2FCODE%3E.%20This%20is%20the%20root%20application%20which%20is%20created%20when%20you%20add%20a%20new%20website.%20If%20you%20have%20a%20specific%20application%2C%20make%20sure%20to%20use%20it%20in%20your%20config%20files.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-895478871%22%20id%3D%22toc-hId-957176096%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--1656678090%22%20id%3D%22toc-hId--1594980865%22%3EPrevent%20inheritance%3C%2FH2%3E%0A%3CP%3EYou%20may%20want%20to%20allow%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20files%20to%20be%20used%20in%20subfolders%20but%20you%20don%E2%80%99t%20want%20certain%20settings%20to%20be%20inherited%20by%20subfolders.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20your%20root%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20states%20that%20the%20only%20default%20document%20is%20%3CCODE%3Eindex.htm%3C%2FCODE%3E.%20However%2C%20your%20subfolder%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20states%20that%20the%20only%20default%20document%20is%20%3CCODE%3Eiisstart.htm%3C%2FCODE%3E.%20If%20there%20is%20no%20such%20file%20%3CCODE%3Eiisstart.htm%3C%2FCODE%3E%20in%20the%20subfolder%2C%20root%20setting%20which%20is%20%3CCODE%3Eindex.htm%3C%2FCODE%3E%20will%20be%20inherited%20and%20used.%20In%20order%20to%20prevent%20root%20settings%20to%20be%20inherited%2C%20use%26nbsp%3B%3CCODE%3EinheritInChildApplications%3C%2FCODE%3E%20attribute%20in%20%3CCODE%3Elocation%3C%2FCODE%3E%20tag.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%26lt%3Blocation%20path%3D%22%22%20inheritInChildApplications%3D%22false%22%26gt%3B%0A%20%20%20%26lt%3Bsystem.webServer%26gt%3B%0A%20%20%20%20%20%20%26lt%3BdefaultDocument%20enabled%3D%22true%22%26gt%3B%0A%20%20%20%20%20%20%20%20%20%26lt%3Bfiles%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%26lt%3Badd%20value%3D%22index.htm%22%20%2F%26gt%3B%0A%20%20%20%20%20%20%20%20%20%26lt%3B%2Ffiles%26gt%3B%0A%20%20%20%20%20%20%26lt%3B%2FdefaultDocument%26gt%3B%0A%20%20%20%26lt%3B%2Fsystem.webServer%26gt%3B%0A%26lt%3B%2Flocation%26gt%3B%26nbsp%3B%3C%2FPRE%3E%0A%3CP%3EThe%20location%20path%20in%20the%20example%20above%20is%20empty.%20It%20means%20that%20this%20setting%20will%20be%20applied%20to%20the%20level%20of%20the%20config%20file%20and%20below%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMore%20information%20about%20%3CSPAN%3EinheritInChildApplications%3C%2FSPAN%3E%3A%20%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fen-us%252Fdotnet%252Fapi%252Fsystem.configuration.sectioninformation.inheritinchildapplications%253Fview%253Dnetframework-4.7.2%26amp%3Bdata%3D02%257C01%257CNedim.Sahin%2540microsoft.com%257C86fa31fd1bef48fec5ea08d65b96e4fa%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636797100985657455%26amp%3Bsdata%3DzkZxuDyGCPYgouPSmdsqKjmChRkYWeFHd0SzkNgSYBc%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ELink%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-297627%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EThe%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln4%22%3E%3CCODE%3Eweb.config%3C%2FCODE%3E%3C%2FSPAN%3E%20%3CSPAN%3Efiles%20in%20application%3C%2FSPAN%3E%20%3CSPAN%3Esubfolders%20can%20override%20the%20settings%20of%20the%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln4%22%3E%20%3CCODE%3Eweb.config%3C%2FCODE%3E%3C%2FSPAN%3E%20%3CSPAN%3Efiles%20in%3C%2FSPAN%3E%20%3CSPAN%3Eparent%20folders.%20In%20order%20to%20prevent%20parent%20config%20files%20to%20be%20overwritten%2C%20you%20need%20to%20configure%20IIS%20to%20ignore%20%3CCODE%3Eweb.config%3C%2FCODE%3E%20files%20in%20application%20subfolders.%20Follow%20the%20steps%20in%20this%20post%20for%20the%20related%20IIS%20configuration.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

The web.config files in application subfolders can override the settings of the web.config files in parent folders. In order to prevent parent config files to be overwritten, you need to configure IIS to ignore web.config files in application subfolders. 

 

An example for configuration hierarchy in IIS is below. In this example, there is a website called Site1. This website has two subfolders: SubDir1 and SubDir2.

IIS-configuration.PNG

Diagram: A sample IIS configuration hierarchy

 

Example

In this example, If you don’t want the web.config files in SubDir1 and SubDir2 folders to override settings in the web.config file of Site1 folder, use allowSubDirConfig attribute in your applicationHost.config file. For instance:

 

<sites>
   <site name="Site1" id="1">
      <application path="/">
         <virtualDirectory path="/" physicalPath="D:\WebApps\Site1" allowSubDirConfig="false" />
      </application>
   </site>
</sites> 

For enabling this setting in all websites hosted by IIS, use virtualDirectoryDefaults tag: 

<sites>
   …
   <virtualDirectoryDefaults allowSubDirConfig="false" />
</sites>

Note: allowSubDirConfig attribute can be used only in applicationHost.config. It is not available for web.config files. 

Ignore config files for certain subfolders only

You can also configure IIS to ignore web.config files in certain subfolders only. However, these folders has to be virtual folders. In the example below, web.config files are ignored in all subfolders except the one in subDir2.

  

<sites>
   <site name="Site1" id="1">
      <application path="/">
      <virtualDirectory path="/" physicalPath="D:\WebApps\Site1" allowSubDirConfig="false" />
      <virtualDirectory path="/SubDir2" physicalPath="D:\WebApps\Site1\SubDir2" allowSubDirConfig="true" />
      </application>
   </site>
</sites>

Please note that the examples above use <application path="/">. This is the root application which is created when you add a new website. If you have a specific application, make sure to use it in your config files.

 

Prevent inheritance

You may want to allow web.config files to be used in subfolders but you don’t want certain settings to be inherited by subfolders.

 

For example, your root web.config states that the only default document is index.htm. However, your subfolder web.config states that the only default document is iisstart.htm. If there is no such file iisstart.htm in the subfolder, root setting which is index.htm will be inherited and used. In order to prevent root settings to be inherited, use inheritInChildApplications attribute in location tag.

 

<location path="" inheritInChildApplications="false">
   <system.webServer>
      <defaultDocument enabled="true">
         <files>
            <add value="index.htm" />
         </files>
      </defaultDocument>
   </system.webServer>
</location> 

The location path in the example above is empty. It means that this setting will be applied to the level of the config file and below it.

 

More information about inheritInChildApplications: Link