简体   繁体   中英

Asp.net HttpModule in directory level web.config

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 just a folder, and within that folder it has its own web.config. I'm doing this so each application has its own application related contents, stylesheets, configs etc.

Now I created a custom http module. When adding this to the root web.config, the module is working properly. When adding the http module config to the directory-level web.config (eg /Applications/MyApplication/web.config) the module is not initialized anymore. Even though the msdn states that the HttpModules config element is also working at directory level. Anyone knows how to solve this?

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.

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.

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.

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()
    {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM