Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs
Contact   •   Articles   •   Products   •   Support   •   Search
Ad-free experience sponsored by:
ASPOSE - the market leader of .NET and Java APIs for file formats – natively work with DOCX, XLSX, PPT, PDF, images and more

IIS/ASP.NET Settings and Virtual Directory Inheritance


Here's one thing that that irritates me to no end - the way IIS and ASP.NET inheritance works down from a root directory into child directories.

I started a new project today and immediately I was greeted by about 15 configuration errors. Like this:

Server Error in '/WebStore2008' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'Westwind' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

 
Line 30:     <pages>
Line 31:       <namespaces>
Line 32:         <add namespace="Westwind.Tools" />
Line 33:         <add namespace="Westwind.Web.Controls" />
Line 34:       </namespaces>

Source File: c:\Westwind\Web.Config    Line: 32



This is of course only one in a whole string of errors that will come up one at a time. This particular error is due to a BIN deployed component that doesn't exist in my newly created application and the app is expecting the namespace which of course isn't actually there.

So this is a brand new project why these errors? Well, the problem is that the newly created app automatically inherits the root web's config settings. So if you use any custom modules, handlers, or as above even add a namespace, references that are not used in a new project down the virtual directory you'll get errors because those settings usually don't make sense in the lower virtual directories.

You can fix this by explicitly overriding adding a clear tag:

    <pages>
      <namespaces>
        <clear />
      </namespaces>
    </pages>

But this can be a royal pain if you have to do this on 10 or 20 different sections of the configuration for each application downwind of the root virtual. If you have 10 or 20 virtuals this gets old real quick, and it continues to be volatile if any changes are made to web.config in the root later on which can basically bring down all the applications on a server.

Don't think this is a big problem? Consider that configuration just got a lot more complex with the introduction of IIS 7 into the mix where you now have to worry about mixtures of IIS 7 settings (in system.webserver) in addition to the settings in system.web. Worse consider the slew of 'default' configuration settings that a .NET 3.5 app requires (think of the MS Ajax includes, handlers and modules, plus the 3.5 libraries) and imagine all that gunk in a root application. If you have a 2.0 virtuals underneath the root that things will get real ugly.

Stop Inheritance cold with <location inheritInChildApplications="false">

Thankfully there's a not very obvious way to work around this whole issue by using a location tag. Using the inheritInChildApplication attribute in the location element allows you to basically stop all inheritance dead from the current application down. All that's required is that the settings you want to protect are stored inside of a <location> element like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
    <section name="webConnectionConfiguration" type="System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <section name="wwBanner" type="System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <connectionStrings>
    <add name="WestWindAdmin" connectionString="server=.;database=WestWindAdmin;integrated security=true;" providerName="" />
  </connectionStrings>

  <location inheritInChildApplications="false">
    <system.web>
      <compilation debug="true">
        <assemblies>
          <add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        </assemblies>
      </compilation>
      <pages>
        <namespaces>
          <add namespace="Westwind.Tools" />
          <add namespace="Westwind.Web.Controls" />
        </namespaces>
      </pages>
    </system.web>
    <system.webServer>
      <handlers>
        <add name="wst West Wind Web Connection" path="*.wst" verb="*" modules="IsapiModule" scriptProcessor="c:\westwind\wconnect\wc.dll" resourceType="Unspecified" />
      </handlers>
      <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>
  </location>
  
  <wwBanner>
      <add key="ConnectionString" value="server=.;database=WestwindAdmin;integrated security=true;" />
      <add key="BannerManagerHandlerUrl" value="~/wwBanner.ashx" />
      <add key="TrackStatistics" value="True" />
      <add key="HomeUrl" value="~/admin/" />
    </wwBanner>
</configuration>

You can basically wrap the location tag around any entries you don't want to inherit or even the entire web.config below the Configuration tag. You can also leave settings that you might actually want to inherit all the way down outside of the location tag. Thus, in the above the connectionStrings and wwBanner configuration settings are visible to child virtuals.

Why oh, Why?

I've run into this issue on a number of occasions and this particular option isn't easy to find. The web.config Intellisense and schema preview doesn't show this attribute so it's easy to miss. In addition, when I have looked for this thing I always forget exactly where it is applied: on the Location tag.

This solves the problem neatly.

I really wish that IIS 7 provided a global option in its settings to simply stop inheritance at the root Web level for any entries automatically. Honestly I don't see why you'd ever want inheritance to jump virtual directory boundaries. There's no good reason for the inheritance in the first place not when you have machine level configuration settings. When I set up a virtual directory I want to be insured that I have a clean configuration and not have to worry about config settings higher up the chain.

But at least this problem has a solution even if it's one that I'll forget about probably a few times more. <s> Maybe writing it up here will help jog my memory...

Posted in ASP.NET  IIS  IIS  

The Voices of Reason


 

Jon Tørresdal
August 11, 2007

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Nice work! I've been looking for something like this for quite some time. However, we've used config inheritance a couple of places in our applications and found it very useful. One example is on one of hour application servers, were we have running many web services in different VD's. We've added some common config settings on the root web site. This makes it very easy for admins to change settings for the web services in one place. The downside of this is of course if some other VD is installed on this server, they will inherit our settings...

PBZ
August 11, 2007

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Hi,

I usually create a complete new website (maybe even assign a different pool) and make a virtual directory to it from the "main" website. I can't test right now, but I believe it doesn't propagate the settings if you do it this way -- I could be wrong.

Peter.

Bryan Peters
August 11, 2007

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

thank you, thank you, thank you! This issue has been bugging me for the last year on any project that I use UrlRewriting.net on the root folder. It's been driving me nuts!

Rick Strahl
August 11, 2007

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

@PBZ - Unfortunately the inheritance happens even if your virtuals live in a physical location other than underneath the root. For a while I had thought that would solve the problem of inheritance but it doesn't actually.

@Jon - I'm curious - wouldn't it make more sense to make administrative settings like that in the machine level web.config?

Jano's Software Ramblings
September 09, 2007

# Hosting Apps in Virtual Directories in Godaddy

Hosting Apps in Virtual Directories in Godaddy

Amit
October 18, 2007

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Actually my Asp form run over local IIS,now i would like to host on web site,but following error are display,did needed any Changes in code.
How to configure virtual directory ?

============
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

Source Error:

Line 24: ASP.NET to identify an incoming user. Line 25: --> Line 26: <authentication mode="Windows"/> Line 27: <!-- Line 28: The <customErrors> section
enables configuration

Matthew
April 05, 2008

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Thank you.

Don't know what it is but I come across your site about 2wice a week researching these type of pains in the ass.

Marius Els
April 11, 2008

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

I stumbled on to this problem only a day ago and baffled me; but I found this post via google and applied the "<location inheritInChildApplications="false">" to the default web site's web.config file and it solved my problem! I just had to play around putting the tag in the correct place in the file. Thanks

Guy Harwood
July 08, 2008

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

you just saved my bacon, nice one!

wiki directory
July 29, 2008

# wiki directory

Create a file named " app_ controller. php " under the app/ directory.

Mike Borozdin
August 11, 2008

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Thanks a lot Rick! This post has really helped me! Even though it is exactly a year old, it's still useful!

Matt
October 13, 2008

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

dude, how is that you have the answer to 95% of my "what the h@ll is Microsoft thinking and how in the world is this not a problem for everyone else" questions? You are a genius...not sure what I would do without you!

Fabian
November 02, 2008

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

I having problems with system.web.extension "ambiguous" (main website use net 2.0 with system.web.extension 2.xxxx and new VD use a 3.5 version) and this solution saved my day :) thanks!

emailandthings
November 28, 2008

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Too bad this doesn't apply to "<configSections>"... That is, say you have a config section you want to override on the child, <location> won't help you. And yes, I tried "remove", and "clear"...

Jas
December 02, 2008

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Thankyou for the info. Just what I was looking for !

Ralph Whitbeck
February 20, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Rick,

This helped me with my web.config issues in a VD.

Thanks for the info.

Ralph

Mick
March 12, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Outstanding! Thank you Rick for sharing the information in this post. Indeed it is not widely known and I greatly appreciate your help and getting the message out.

The devva.net blog
April 29, 2009

# How to prevent web.config inheritance in ASP.NET

How to prevent web.config inheritance in ASP.NET

Bruce
May 21, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Thank you, thank you, thank you!!!

Adesh Kumar
August 31, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Great solution. It worked fine for me.
I am running asp.net 3.5 website as virtual folder in asp.net 2.0

Val
September 15, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Thanks so much! This solution is logical and was just what I needed. I used both the location inheritInChildApplications="false" in the parent and a configSection <clear /> in the child app. Each worked just as described.

Anonymous
October 15, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Ok, but you can still get inheritance problems with <configSections> so problem may likely not be solved still. You can't wrap that section with the <location> tag above or use <clear/> either so what do you do? I have not found a solution to anything conflicting outside of <system.web> if wrapped in <location>

Wolcott
October 19, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Has anyone found a solution to not inherit the roots <configSections>? Added <clear /> to the child web.config just after <configSections> seems to be ignored.

Petar
November 30, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Hi, using this solution on IIS 7.0 gets me: "HTTP Error 500.22 - Internal Server Error
An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode." Is there any solution when using Intergrated mode? Thanks.

andrew long
December 23, 2009

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

thank you, thank you, thank you! this has been driving me nuts for some time

Matt Freeman
February 11, 2010

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Careful though, wrapping the system.web tag in the location tag, although works, it will stop VS2008 from being able to do F5 debugging.

Gary
February 16, 2010

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

This does not work if the application is older than Framework 2.0. The "location" attribute is not recognized in 1.1.

fschwiet
February 24, 2010

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Dude I don't know how you do it but so often when I google an ASP.NET related problem I find the solution on your blog. Thanks.

Bruce Adams
August 17, 2010

# Prevent web.config inheritance in ASP.NET child virtual directories and applications

Prevent web.config inheritance in ASP.NET child virtual directories and applications

Bruce Adams
September 13, 2010

# Prevent web.config inheritance

Prevent web.config inheritance

Robert
October 20, 2010

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

You can fix the debug issue by moving the <compilation/> element back out of the <location> element. I wrestled with this one for a long time...

Luigi
November 30, 2010

# re: IIS/ASP.NET Settings and Virtual Directory Inheritance

Does anybody know if there´s any solution for the Section inheritance issue. I have a two isolated apps, the first running at the root, and the second one running as a Virtual Directory inside the first one.

It happens that both, have a Section defined with the same name but pointing at different assemblies. I cannot duplicate the binaries.

I got rid of the other issues by using Location, commenting inner Runtime and Compilation, but can´t get thru this.

Thanks.
Luigi
 

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2017