user2184057 user2184057 - 14 days ago 8x
C# Question

Application deployed in azure using wrong web.config

I've got an azure website (MVC) configured and everything was working fine until i tried adding a WebAPI project to solution which is deployed to azure.

My virtual directory mapping looks like

  1. virtual ----- physical --- IsApplication

  2. / ----- site\wwwroot\ ------ IsApplicaiton (yes) [Path for mvc
    project] Application

  3. /api ---- site\services\ ------- IsApplication (no)

  4. /api/code ------ site\services\code\ ------- IsApplication (yes) [Path for WebAPI project]

The MVC application is working fine but the WebAPI application (which is blank scaffold from VS WebApplication -> WebAPI2) is not. After modifying WebApi's
to disable custom errors page I found out that the problem is the namespace included in MVC application's
(of course WebAPI project does not possess reference to it and it shouldn't). But why does WebAPI even bother itself with
sitting in MVC application when it has its own?


Web.config settings "waterfall" down within applications. So if you have an application hosted at the root '/' and then another application hosted under that (i.e. '/api/code') then all of the settings from the root web application's web.config will be added into the settings of the sub application's web.config. The physical location of the application is irrelevant to this behavior. All of this is driven by the virtual directory structure. This is why you are seeing errors related to your MVC app's namespace in your WebAPI app.

To fix this you should use <remove name="..." /> or <clear /> tags or inheritInChildApplications attribute in your WebAPI's web.config to remove settings that are passed down from your MVC application. An example of how to do that can be found in this SO answer:

For more information on web.config inheritance you can check out this MSDN article too: