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.

General Code Snippets ASP.NET
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:15PM

Comments

7/11/2007 11:24:06 AM
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
8/19/2007 4:41:15 AM
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?
2/7/2008 3:53:37 PM
Thanks for sharing this, just what I was looking for! Great problem solver for inheritance of themes!
2/28/2008 1:32:15 AM
Thanks for sharing. This has saved me a ton of time!
3/16/2008 4:25:58 AM
谢谢你!
5/22/2008 4:33:11 PM
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!
7/3/2008 5:39:17 PM
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! :)
7/10/2008 1:15:41 PM
THANKS! Searched the world over for a answer.

Skip
7/11/2008 2:52:09 PM
Gr8 solution...
Its work for application
9/1/2008 7:19:29 AM
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 :)
9/3/2008 3:56:14 PM
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.
9/6/2008 10:26:31 PM
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!
10/9/2008 10:58:32 AM
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!
Ed
Ed
11/24/2008 5:45:48 AM
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! ;)
2/3/2009 5:14:26 PM
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.
2/21/2009 5:45:23 PM
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?
2/21/2009 5:46:34 PM
...the Remove tag:
<remove name="namespace.Properties.Settings.MyConnectionString" />
3/31/2009 10:25:54 AM
where is web.config file :(( I dont know please help me....
3/31/2009 1:27:50 PM
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...
4/10/2009 3:08:04 PM
Awesome! Thanks!
4/23/2009 10:06:59 AM
How should you disable inheritance of ? This only applies to entries in
4/23/2009 10:08:00 AM
How should you disable inheritance of < configSections />? This only applies to entries in < System.Web />
7/1/2009 6:47:24 AM
Good answer, I am looking for the solution of the same question. Find the movies or mp3 you are looking for at music-and-soundtracks.com the most comprehensive source for free-to-try files downloads on the Web
8/20/2009 2:47:14 PM
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
3/4/2010 9:23:44 AM
hello,
I couldn't find "inheritInChildApplications" attribute of the Location tag, rather I could get only "allowOverride" attribute.

plz let me know how and where can I find this.

Thanks & regrds,
Farida
4/9/2010 1:47:14 AM
I tried this, but the location tag DOES NOT HAVE a an inheritInChildApplication attribute
12/27/2010 10:21:27 AM
Thank you ! :)
2/9/2011 6:57:14 PM
Thank you so much for this tip.
TV
4/29/2011 6:01:05 PM
Wonderful! Thank you! For people indicating there is no inheritInChildApplication attribute on the location tag, it actually does exist and work, but Visual Studio won't show you for some reason and will complain. Just ignore the complaints.
6/25/2011 6:33:43 AM
Drug Abuse In Teens Statistics http://www.thecouponclass.com/ - soma 350 mg Soma is also used to treat many musculoskeletal conditions when a doctor finds that it is the proper medication for the treatment of the condition. http://www.thecouponclass.com/ - soma no prescription
6/25/2011 10:04:00 PM
Drugs For Depression And Anxiety http://www.supercheapfags.com/ - generic eszopiclone You should not take Lunesta if you do not have at least seven or eight hours to devote to sleeping. http://www.supercheapfags.com/ - buy lunesta online
6/26/2011 1:50:13 AM
Viokin Medicine http://www.walkdiggler.com/ - plavix 75 mg Not all patients will experience these side effects and most find that they subside on their own within a few days. cheap plavix online

No new comments are allowed on this post.