Asp.net HttpModule in directory level web.config

后端 未结 4 1017
心在旅途
心在旅途 2021-02-04 10:31

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

相关标签:
4条回答
  • 2021-02-04 10:46

    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.

    0 讨论(0)
  • 2021-02-04 10:58

    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.

    0 讨论(0)
  • 2021-02-04 11:03

    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.

    0 讨论(0)
  • 2021-02-04 11:04

    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()
        {
        }
    }
    
    0 讨论(0)
提交回复
热议问题