Scott Hanselman

Changing ASP.NET web.config inheritance when mixing versions of child applications

March 26, 2013 Comment on this post [19] Posted in ASP.NET | Bugs | IIS
Sponsored By

Mixed Application Pools

My blog and all the sites in and around it are a mix of .NET 2.0, 3.5 and 4. This blog engine is currently .NET 3.5 and runs at http://hanselman.com/blog, but the application at http://hanselman.com/ (the root) is .NET 4.

You can happily mix and match applications across .NET versions on a single IIS instance. You can see how mixed my system is in the screenshot at right there.

However, things got messy when I changed the parent / application to .NET 4, but kept the child /blog as .NET 3.5 (the 2.0 CLR). I got lots of errors like

  • Unrecognized attribute ‘targetFramework’. Note that attribute names are case-sensitive. The targetFramework attribute was inherited from the root .NET 4 Web.config file in the Default Web Site root using ASP.NET configuration inheritance and confused the /blog .NET 2 application.

I didn't want to change the /blog applications' web.config. I just wanted to stop it from inheriting the settings from the parent application. Turns out you can wrap whole sections in a location tag, and then tell that scoped tag to prevent child applications from inheriting.

What you do is change the parent .NET 4 app's web.config to indicate its settings shouldn't flow down to the children, like the .NET 2/3.5 /blog app.

<location path="." inheritInChildApplications="false">
<system.web>
...your system.web stuff goes here
</system.web>
</location>

You can actually read about this in detail in the ASP.NET 4 "breaking changes" documentation. Of course YOU read those closely, don't you? ;)

I chose to change this settings for all of System.Web, but you could do it on a per-section basis if you preferred.

Hope this helps you!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
March 26, 2013 15:05
So, does that mean we can put something as simple as

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

in the parent web.config and not have unrecognised attribute explosions? If so, you rock!
March 26, 2013 15:44
Is it possible to do it the other way around? I find it strange that I have to update the parent, when it is the child who decides if the settings should be inherited or not.

March 26, 2013 17:14
That also means that none of the children will be able to inherit the settings from parent/root.
March 26, 2013 18:52
It should be noted any setting NOT contained with in the location will pass down to all child applications. We've had to find this out the hard way with a SSO application that we recently wrote.
March 26, 2013 19:13
Be aware that the inheritInChildApplications attribute is not part of the web.config schema (even in VS 2012) so you will get blue squigglies under it with a message of "The 'inheritInChildApplications' attribute is not allowed." It will, however, work correctly.
March 26, 2013 22:51
Scott Lance, Yanesh - True. Things inside are blocked, and things outside are passed down. If you want you can scope some and not scope others on a case by case basis.

Craig - Yes, I'm working on getting it added.
March 26, 2013 23:24
I suppose I should fess up. I've mixed .net 4.0 and 2.0 applications in the manner described here. But the unfortunately thing is its not always an option to turn off inheritance in the way you describe (I will admit this is by far the preferred option).

It is still possible to have a .net 2.0 root and have .net 4.0 applications beneath the root (think a Sharepoint 2007 instance with .net 4.0 mvc applications beneath it).

You have to do this however by all sorts of nasty messing around in the root web config and possibly (depending on what you are using) also using assembly redirection in the child.

We had to do this because there is no way to say for just one child to not inherit, you have to say "i'm not inheritable).

March 27, 2013 0:22
If I have an "application", it shouldn't inherit from a parent folder in a different "application". That would seem to be the whole point of calling different folders "applications", wouldn't it?

Microsoft totally screwed up here by making folder structure more important than "application" designation.

Also, each app pool should have it's own machine.config for that .NET version.

That would have been the proper way to handle all this. Everything else is just a slimy kludge on top of a bad design.
March 27, 2013 0:49
Have to agree with PRMan and krystan. Although I appreciate the ability do this, it's never straight forward. It's not as easy as just putting a single location tag in the parent web.config, and it really should be.
March 27, 2013 14:22
Welcome to the world of constantly checking your web.config to see what mistakes programs make when they edit your web.config automatically.

For example:

- Load IIS
- Click on your website
- Load .NET Error Pages
- Change the setting in here
- Look in your web.config to see what it's done!

In my case it creates a new system.web at the bottom. I've battled with this for over 5 years.

March 27, 2013 16:45
Scott,

Is it possible to make main runs in .Net 2.0 Frame work and blog as .Net 4.0 Framework with out using sub web.config ?
March 29, 2013 14:00
hey Mr.Scott I wanna learn Asp.net from you. Will you teach me...Please.
I am a freasher frankly speaking
March 29, 2013 23:22
Nice article. I usually don't comment, But Chandan Kumar made me to! If you want scot to teach you ASP.NET, You should pay him more than what Microsoft does!
March 30, 2013 0:13
Hi Scott.
Can you reach Azure team and ask them why they don't let people deploy apps to Azure (ASP.NET Web Site with small SQL Server DB) for free? I find it weird that guys like AppHarbor can do it but MS can't. I think that could convince more people to pay for other Azure services.
March 30, 2013 6:50
This is really great stuff great article
March 30, 2013 11:18
Well, "Are you kidding," a 100 meg SQL database is $5 and a 1 GIG SQL database is $9.99, which is pretty cheap.

But for websites like thisdeveloperslife.com, ratchetandthegeek.com and a dozen others I use SQL Compact Edition for free. It's worked great for almost a year.

Does that help?
April 01, 2013 18:53
I haven't used SQL Compact before. Maybe that is an option.
Maybe 5$ isn't much but 60$ a year for app that is built just for fun is not sth I'm ready to pay.
April 02, 2013 22:35
AreYouKidding - Remember, you get 10 free websites on Azure. You could make fun sites for free with SQL Compact.
July 29, 2013 17:43
Currently I have some configuration issue with asp.mvc 4 web application deployed under root asp.net webforms 2.0 application (of course this is not best option to work with at all, but it is what it is).
The most interesting part - that application works in the local UAT environment (configured exactly the same as the production) and in one production server, on the second production server it gives an error about wrong configuration (even if the deployed code seems to be exactly the same on both instances. We've been fighting with this issue for some time - but no luck :( .

Honestly speaking - I don't want to introduce changes into root web.config = as new child application is not the only one hosted under same root and it somehow can affect other children (at least there's a chance).
Currently we're going to reinstall app from scratch on the broken server, and I'm willing to introduce changes only into child web.config. In case it's not going to help - I'm simply considering to move away from web application approach (under root item) and have it deployed as standalone web site (just to remove additional dependencies with parent).

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.