How to stop inheritance of Web.Config files

By default, the Web.Config settings placed in a root or parent folder are inherited by child folders. This is true even for virtual directories and Application sites with their own local Web.Config.

Problem

At first, this seems helpful. But when developing with a local IIS installation on a workstation OS, you may quickly find this becomes problematic. You may have multiple sites running locally for development reasons that will be stand-alone sites once deployed. You later discover that the deployed site no longer works since it now missing the values it was inheriting from a parent Web.Config.

An example could be where a connection string was defined in the root application and was inherited to your child Web.Config. Once the child site is deployed away from the parent, it fails as the entry is missing. What we want is to have more control over other Web.Config files and exactly what they are inheriting from above, if anything.

Work-Arounds

 In your local development environment, you may want the same named attribute in the child Web.Config but with a different value. But then ASP.NET complains because that name is already defined in the parent. You can then use the Remove and Clear tags to effectively override the parent entry. Remove effectively removes the named value so you can then redefine it with an Add tag. Clear will effectively clear all of the inherited entries in that section.

A quick fix could be to use the Remove tag so you can re-add the entry with the same name but different values.

      <connectionStrings>
         <remove name=MyDBConnString” … />
   
     <add name=”MyDBConnStringconnectionString=Blah  />
   
  </connectionStrings>

You can also use the Clear tag as well but this will effectively remove all inherited entries in that section.

       <connectionStrings>
         <clear /> 
      </connectionStrings>

Stop Inheritance Completely

If you would prefer to simply stop all inheritance and know that each Web.Config stands alone and contains everything it needs when you deploy, try this next suggestion. Adding this code to your root Web.Config should prevent any values from being propagated down to to other sites. This gives you the confidence that any individual site can by deployed to a server and the Web.Config will not be missing needed entries.

Use the <location> tag with an attribute of inheritInChildApplications equal to false in your root Web.Config file.

  <location path=”.” inheritInChildApplications=”false“>
    <system.web>
    …
    </system.web>
  </location>

You can wrap a single section of your web.config file in <location> attribute or you could enclose the entire <system.web> as shown above. This will cause ASP.NET to not inherit settings from that section to child applications or folder web.config files.

Comments

  1. Jesus Cifuentes says

    Funciona!! muy buen articulo

  2. Hello,
    I have been trying for days to get this to work.
    Can you see what is wrong with this code in my web.config

Speak Your Mind

This site uses Akismet to reduce spam. Learn how your comment data is processed.