How to use web.config in website sub-folder

Do you want to use different web.config in the website folder?

For example we want to use a different app key value in a subfolder code file but we already have the same name key in root folder web.config.

For Example
Root web.config

 <add key="examplekey" value="123" />
Sub-folder  web.config
 <add key="examplekey" value="456" />

If your sub-folder code files don’t have web.config then it will continue to use root web.config value.

So in order to have Sub-folder its own web.config and key then you should follow following instructions

In the root’s folder web.config  wraps the <system.web> element with the following element: <location path=”.” inheritInChildApplications=”false”></location> and leave all other settings/elements unchanged.

For Example

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

In sub-folder web.config only have the following

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="examplekey" value="456" />
  </appSettings>
  <connectionStrings />
</configuration>

This way you sub-folder code file will start using new value from the sub-folder web.config

We also like following article on sub-folder web.config

https://techcommunity.microsoft.com/t5/iis-support-blog/how-to-prevent-web-config-files-to-be-overwritten-by-config/ba-p/297627

Read more about IIS settings https://blog.certificationskart.com/category/windows/iis/

Leave a Reply

Your email address will not be published. Required fields are marked *