07 November 2008

Multiple web applications

Disabling web.config Inheritance for Child Applications in Subfolders in ASP.NET 2.0!!
(making multiple web applications work together)


Each ASP.NET Web Application has its own configuration file called web.config file.
In fact every directory in ASP.NET application can have one. Settings in each web.config file apply to the pages in the directory where its placed, and all the subdirectories of that directory.

This is called Configuration Inheritance.

So if you create an ASP.NET application and set its web.config file, add custom HttpHandlers, UrlRewriting module etc and try to create another ASP.NET Web Application in the subfolder, you can end up having problems because application in the subfolder will inherit all the settings from its parent web.config.

So if you for example setup UrlRewriter module in your root web applications like this:

<httpModules>

<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>

<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</httpModules>

And in your child web application (in subfolder) you are not using UrlRewriteModule, then if you try to run the child Web Application in your browser, you will get error like this:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Could not load file or assembly 'UrlRewritingNet.UrlRewriter' or one of its dependencies. The system cannot find the file specified. (d:\Projects\VS\AspDotNetFaqProject\Website\web.config line 89)

Source Error:

Line 88: <httpModules>
Line 89: <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>
Line 90: <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,

System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
Line 91: </httpModules>


What happens here is that because UrlRewriteModule is configured in the parent's folder web.config file, this setting is inherited by the child application's web.config file, and because of this ASP.NET is looking for the UrlRewriteModule DLL file in the BIN directory, and off course its not there.

Luckily, there is a easy solution to this problem.

First thing you can do is to remove the problematic HttpModule in your child application web.config file using the remove command like this:

<httpModules>

<remove name="UrlRewriteModule" />

</httpModules>

This would remove the handler and your application would run fine.
Or you could use <clear/> command like this:

<httpModules>

<clear/>

</httpModules>

This would clear all the HttpModules in your child application.

But what to do when there are many settings in your root web.config file that you don't want to be propagated to your child applications?

Here is the solution:
With the <location> attribute with inheritInChildApplications set to false in your root web.config file you can restrict configuration inheritance.
So, by wrapping a section of your web.config file in <location> attribute you can instruct ASP.NET not to inherit those settings to child applications/folders and their web.config files.

In fact you can wrap the whole <system.web> section in one <location> attribute and disable configuration inheritance so none of the parent settings from <system.web> will be inherited to the child applications you create in subfolders.

Here is how to use this in practice:

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

<system.web>
...

</system.web>
</location>

NOTE: Do remember that if you do this, you need to manually insert all the settings you need in the <system.web> for your child applications because none of the settings from the root web.config will be propagated...

No comments: