I created a custom http module and want to add this module to the web config. The web application is a project that contains several \"sub applications\". A sub application is j
To echo Marvin Smit's comment, it seems that configuring <modules>
under a <location>
in web.config
simply does not work - any modules specified in this fashion are NOT invoked.
What you can do is to specify the module at root level, and have it controlled by an appSetting
, which can be hierarchically specified and overridden as required:
<configuration>
<appSettings>
<add key="UseCustomModule" value="false"/>
</appSettings>
<location path="MyFolder">
<appSettings>
<add key="UseCustomModule" value="true"/>
</appSettings>
<system.webServer>
<modules>
<!-- CANNOT add module at this level, hence the overridden appSetting -->
</modules>
</system.webServer>
</location>
<system.webServer>
<modules>
<add name="CustomnModule" type="MyApplication.CustomModule" />
</modules>
</system.webServer>
</configuration>
Then within the code for CustomModule
:
private static bool ModuleEnabled()
{
bool appSetting;
if (!bool.TryParse(ConfigurationManager.AppSettings["UseCustomModule"],
out appSetting))
appSetting = false;
return appSetting;
}
ASP.NET will see to it that the appropriate value of UseCustomModule
for our current location is the one we read.
Would it not be possible to create a custom config section that lists out the directories you want to include or exclude your module behaviour? Your module could then inspect that to see if it should do it's work based on the request URL.
I know that's not quite what you are asking, but would certainly give you the behaviour you need.
In IIS under your root application select your folder which has own web.cofig with HttpModules defined, right click and select property, on Directory tab click on Create button.
It will create sub application and now HttpModules should work.
Base HttpModule for this case could be the following:
public abstract class PathBasedHttpModule : IHttpModule
{
public abstract void Init(HttpApplication context);
protected EventHandler BuildConditionalEventHandler(Action<object, EventArgs> targetHandler)
{
EventHandler action = (sender, args) =>
{
var settingsValue = CloudConfigurationManager.GetSetting(ModuleEnabledAppSettings);
if (!string.IsNullOrEmpty(settingsValue) && bool.Parse(settingsValue))
{
targetHandler(sender, args);
}
};
return action;
}
protected abstract string ModuleEnabledAppSettings
{
get;
}
public void Dispose()
{
}
}