Asp.net HttpModule在目录级web.config中

时间:2022-07-30 11:26:41

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.

我创建了一个自定义的http模块,并希望将此模块添加到Web配置中。 Web应用程序是一个包含多个“子应用程序”的项目。子应用程序只是一个文件夹,在该文件夹中它有自己的web.config。我这样做是因为每个应用程序都有自己的应用程序相关内容,样式表,配置等。

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 (e.g. /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?

现在我创建了一个自定义的http模块。将此添加到根web.config时,模块正常运行。将http模块配置添加到目录级web.config(例如/Applications/MyApplication/web.config)时,模块不再被初始化。即使msdn声明HttpModules配置元素也在目录级别工作。谁知道怎么解决这个问题?

4 个解决方案

#1


13  

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.

为了回应Marvin Smit的评论,似乎在web.config中的 下配置 根本不起作用 - 不会调用以这种方式指定的任何模块。

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:

您可以做的是在根级别指定模块,并由appSetting控制它,可以根据需要按层次结构指定和覆盖:

<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:

然后在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.

ASP.NET将确保我们当前位置的UseCustomModule的适当值是我们读取的值。

#2


1  

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.

在根应用程序下的IIS中,选择您自己的web.cofig文件夹,其中定义了HttpModules,右键单击并选择属性,在“目录”选项卡上单击“创建”按钮。

It will create sub application and now HttpModules should work.

它将创建子应用程序,现在HttpModules应该工作。

#3


1  

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.

是否无法创建自定义配置部分,列出要包含的目录或排除模块行为?然后,您的模块可以检查它是否应该根据请求URL执行此操作。

I know that's not quite what you are asking, but would certainly give you the behaviour you need.

我知道这不是你所要求的,但肯定会给你你需要的行为。

#4


0  

Base HttpModule for this case could be the following:

这种情况下的基本HttpModule可能如下:

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

#1


13  

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.

为了回应Marvin Smit的评论,似乎在web.config中的 下配置 根本不起作用 - 不会调用以这种方式指定的任何模块。

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:

您可以做的是在根级别指定模块,并由appSetting控制它,可以根据需要按层次结构指定和覆盖:

<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:

然后在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.

ASP.NET将确保我们当前位置的UseCustomModule的适当值是我们读取的值。

#2


1  

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.

在根应用程序下的IIS中,选择您自己的web.cofig文件夹,其中定义了HttpModules,右键单击并选择属性,在“目录”选项卡上单击“创建”按钮。

It will create sub application and now HttpModules should work.

它将创建子应用程序,现在HttpModules应该工作。

#3


1  

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.

是否无法创建自定义配置部分,列出要包含的目录或排除模块行为?然后,您的模块可以检查它是否应该根据请求URL执行此操作。

I know that's not quite what you are asking, but would certainly give you the behaviour you need.

我知道这不是你所要求的,但肯定会给你你需要的行为。

#4


0  

Base HttpModule for this case could be the following:

这种情况下的基本HttpModule可能如下:

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