Stopping ASP.NET web.config inheritance

If you are only ever running one ASP.NET application on a website this is not an issue. However, if you are running a site which may have an application at the root and other separate applications running in sub or virtual directories, then Settings inheritance could be a problem.

You can read more about how config files get inherited on msdn but here's a tip for stopping settings in the root app from getting inherited. The <location> tag is the only tag I've come across which has the inheritInChildApplications attribute. So to target the main <system.web> just wrap it in the location tag as seen below. 

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

Other Notes:
Although I think the inheritance is in general a good feature to have, especially for inheriting down things such as security settings.  It can even support locking certain settings for child applications, but things can be problematic if the child application doesn't share the same libraries, modules, handlers, masterpages or themes.

Most collections in the web.config have the <remove> tag or <clear> tag to remove irrelevant modules or handlers in the child app.  The problems I've found occur with settings relating to the <pages> tag, which most items in it don't support <remove>. This means if your child applications doesn't share or have the same registered controls, masterpages or themes then you are probably going to have issues or be forced to specify the settings on a per-page basis.  This is where the inheritInChildApplications="false" really comes in handy.

Print | posted on Wednesday, May 16, 2007 1:11 PM

&uot&uot

Comments on this post

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Thank you for the article. This was exactly what I was looking for and MS doesn't seem to give to many hints on the MSDN library about how to stop the inheritance.

Ryan
Left by Ryan on Jul 10, 2007 11:24 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Thanks a lot. My ASP.NET enterprise manager stop working when installing subtext.

My webhosting seems locking CustomError which make me frustated to debug subtext. I want to override the machine.config, is there anyway to do this?
Left by Dityo Nurasto on Aug 18, 2007 4:41 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Thanks for sharing this, just what I was looking for! Great problem solver for inheritance of themes!
Left by Chris on Feb 07, 2008 2:53 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Thanks for sharing. This has saved me a ton of time!
Left by Nathan Harkenrider on Feb 27, 2008 12:32 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
谢谢你!
Left by 湘江 on Mar 15, 2008 4:25 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
I'm new to iis and .net administration and had been having trouble with virtual directories with vendor app's for the last 4 months. Turns out it was the sql role manager we had configured and I threw your location config in and it's solved every problem I'd been having.

Thanks!
Left by tony on May 22, 2008 4:33 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Just two days ago I had a client move my script to avoid inheriting stuff from "parent" site. Problem solved, but now when I know this I will now be able to put this into the default config file and not see this kind of problem again. So thanks! :)
Left by asp.net scripts on Jul 03, 2008 5:39 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
THANKS! Searched the world over for a answer.

Skip
Left by Skip Floyd on Jul 10, 2008 1:15 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Gr8 solution...
Its work for application
Left by Mayank on Jul 11, 2008 2:52 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Great writeup, I didn't know :)
I've bookmarked this post now and will implement this to remove the needs for deploying my UrlRewrite DLL also on the Ajax samples at our site which curently is a need :)
Left by Thomas Hansen on Aug 31, 2008 7:19 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Hello,

Good article.

What if I have 3 applications underneath the main one, and 2 need to inherit the config values, while 1 doesn't. How do I handle that scenario?

Thanks, and best regards,
- Amit.
Left by Amit Bhatnagar on Sep 03, 2008 3:56 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
OMG Thank you! I had a (revenue producing) production application which was dead for over an hour because of this. I has three crappy backup plans, but your site helped me to avoid them all. Thank you, thank you, thank you!
Left by Conrad on Sep 06, 2008 10:26 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Very good tip. I had no idea this was possible but your article is concise and well written. With the use of locations to target directories, it's clear that web.config inheritance can be applied selectively.

Thank you!
Left by Mark on Oct 08, 2008 10:58 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Thank you so much! Exactly what I was looking for.
Left by David Young on Nov 18, 2008 1:40 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Thanks...this works great!

One point...probably obvious to most...

The tag goes in the root or top level web.config file that you do not want the settings to trickle down from.

For some silly reason I focused on the lower level web.config in my virtual folder and the tag did no good there of course. Wasted a few hours before the light bulb finally came on for me! ;)
Left by Ed on Nov 23, 2008 4:45 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Thank you for this post, it’s very important problem for my. Great idea.
Left by cialas on Dec 26, 2008 6:19 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Thanks a lot, i was looking for this solutions since many weeks, but i found many web sites saying inheritInChildApplications, and some saying allowOverride, i would like to actuallyknow why there are two different attributes with opposite meanings ?

That and really helped me solve my problem, i didn;t think about this, probably i was so messed up with the inheritance tags.

Thanks a lot, u saved me tonnes of unproductive time.
Left by harshil patel on Feb 03, 2009 4:14 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
I have an issue with trying to stop ConnectionStrings settings. My top-level and sub apps may reference the same data assembly. However, my top app may need to save to one server where the sub-apps may save to another. Right now, the only solution I could find is to use the


tag if the value differed.
Any shortcuts to stop the connectionstrings section from inheriting?
Left by Jason B on Feb 21, 2009 4:45 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
...the Remove tag:
<remove name="namespace.Properties.Settings.MyConnectionString" />
Left by Jason B on Feb 21, 2009 4:46 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
where is web.config file :(( I dont know please help me....
Left by Mustafa CAKMAK on Mar 30, 2009 10:25 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Hello.
I added this



include web.config file. but I have a question. which path add in "." this area?? May you show me an example path, please...
thanks...
Left by Mustafa CAKMAK on Mar 31, 2009 1:27 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
Awesome! Thanks!
Left by Matt on Apr 10, 2009 3:08 AM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
How should you disable inheritance of ? This only applies to entries in
Left by Robert on Apr 22, 2009 10:06 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
How should you disable inheritance of < configSections />? This only applies to entries in < System.Web />
Left by Robert on Apr 22, 2009 10:08 PM

# re: Stopping ASP.NET web.config inheritance

Requesting Gravatar...
One problem with this solution(currently googling for a solution and read through these comments to see if someone had listed one)-- VS2008 does not properly debug if there's a tag in it.... Try it-- try to debug your parent application with the tag in your web.config, and notice that the debugger never starts... Remove the tag, and voilla...

--
Derek
Left by Derek on Aug 20, 2009 2:47 AM

Your comment:

 (will show your gravatar)
 
Please add 5 and 7 and type the answer here: