How to create a nested application within IIS
- Richard Delgado
- Apr 29, 2018
- 2 min read
Recently I created an application that required that I create a nested application in IIS.
I ran into a few gotchas while deploying an MVC application onto IIS. The MVC application also had an API in a separate project, but I wanted the API to also be used by the MVC application and use authentication/authorization. I did not implement authorization in this scenario yet, but here is what I had to do to get the API working underneath the MVC application.
First, I had to add/update a section to the web.config. We were using the Kendo MVC framework and I kept getting web.config errors (Invalid configuration detected, etc.).
I specifically had to add the following to the parent application: <location path="." inheritInChildApplications="false">
<location path="." inheritInChildApplications="false">
<system.webServer> <!-- ... --> </system.webServer>
</location>
What this will do is tell IIS that everything the section located at the path "." (or the current path) that this configuration information should not be inherited. This is very important because in the case of deploying two different sites/applications, you'll have two different web.config files.
So after creating your application in IIS, you can publish another application in your deployment configuration like so:
App 1 Configuration:
Default Web Site/MvcApplication
App 2 Configuration:
Default Web Site/MvcApplication/Api
Visual Studio generates a default WebApiConfig.cs which contains your default routes.
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );
You'll need to change the mapping from:
api/{controller}/{id}
to:
{controller}/{id}
If you are versioning your API, you should use the route and application accordingly.
[api version here]/{controller}/{id}
Ex:
v1/{controller}/{id}
You would also need to update your api configuration as follows:
Default Web Site/MvcApplication/[api version]
Ex:
Default Web Site/MvcApplication/v1
This is important because this allows you to have a completely separate application pool for your deployed applications. You could create one app pool that recycles all of your applications, but I think it's better to have separate app pools for a a good separation of concerns.
This is pretty much all you have to do in order to handle deploying a nested application.