How to nest applications in IIS

By Mirek on (tags: iis, inheritInChildApplications, web.config, website, categories: None)

Today I will present you a quick tip on how to nest two web applications on IIS 7.5 and avoid the web.config inheritance from the parent application to the child application. Keep reading…

The case is following: we have a main web application that is going to be accesible from root (let’s call it main) and second application which is accesible on path /myapp (let’s call it myapp).

First we need to do is to add main application as a new web site in IIS.
Open IIS Manager and uder Sites node choose “Add Website”. Fill in website details.

IIS-Add new Website

As the hostname provide the actual host name like www.mysite.com If everythink went ok you should now be able to nawigate to your site in the browser.

Next step is to add a myapp application and make it available under the /myapp path. That is when you type in for example http://www.mysite.com/myapp you will access the myapp web application.

In the IIS manager we rightclick on the newly created main web site and select “Add Application”. Provide the application alias which essentially is the path to access the application, set the phsical path and tick the Enable Preload checkbox to speed up application startup.

IIS - add web application

Now we should see following structure tree

IIS-Nested web apps

So we have the website and nested application ready and working, but here comes the problem. If the main website and nested myapp application use same settings in their the web config files it may ends up with an internal server error when we try to access the application. This is caused by settings inheritance. According to MSDN the configuration is inherited from parent web sites to child web applications.
The solution for that is the inheritInChildApplications attribute which tells the IIS to not inherit particular settings in child applications.

All we need to do is to edit main website’s web.config and wrap all conflictig configuration sections with location tag and set the inheritInChildApplications=”false” attribute on it.
This is how the main website’s web.config looks now (for the brewity inner content of setting sections was removed)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  ...
  </configSections>

  <location path="."  inheritInChildApplications="false" >

    <connectionStrings>
    ...
    </connectionStrings>

    <appSettings>
    ...
    </appSettings>

    <system.diagnostics>
    ...
    </system.diagnostics>

    <system.web>
    ...
    </system.web>

    <system.webServer>
    ...
    </system.webServer>

  </location>

  <runtime>
   ...
  </runtime>

</configuration>

That’s it. Now both website and child web application should work without configuration collisions.