Thursday, 29 October 2009

Hiding configuration sections from nested ASP.NET apps

If you have an ASP.NET website nested under another in IIS, you will likely get a server error unless both sites have exactly the same dependencies. This is because IIS, by default, applies web.config settings hierarchically to sub-sites. You can use this fact to your advantage, to prevent you having to repeat the same settings in the child site's web.config.

However, often, you will need certain configuration settings to apply only to the parent site. To restrict specific web.config sections to apply to the parent application only, wrap the relevant sections in the parent site's web config file within:

<location path="." inheritinchildapplications="false">
// your configuration sections
</location>

You can have as many of these location tags in your web.config as you like.

Note: Visual Studio 2005 will throw an error on the inheritInChildApplications attribute. To get round this you will need to add your location elements to web.config after you publish the site (or use VS 2008)