Asp.net的HttpModule目录级别的Web.config [英] Asp.net HttpModule in directory level web.config

查看:116
本文介绍了Asp.net的HttpModule目录级别的Web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的HTTP模块,并希望将此模块添加到web配置。 Web应用程序是一个包含若干子应用的项目。一个子应用程序仅仅是一个文件夹,该文件夹中它有自己的web.config。我这样做,以便每个应用程序都有其自己的应用程序相关的内容,样式的configs等。

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.config,该模块正常工作。当添加HTTP模块配置到目录级的web.config(例如/Applications/MyApplication/web.config)时,模块不再被初始化。即使MSDN指出的HttpModules 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?

推荐答案

要呼应<一href="http://stackoverflow.com/questions/2378586/asp-net-httpmodule-in-directory-level-web-config#comment3300541_2462947">Marvin斯密特的评论,似乎在配置&LT;模块&gt; &LT;地点&gt; 的web.config 简单地不起作用 - 在这种方式中指定的任何模块都不会调用

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.

您的能有什么的做的是指定模块的根目录,并经控制的 appSetting ,可分层指定,按要求覆盖:

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>

然后中的$ C $下 CustomModule

    private static bool ModuleEnabled()
    {
        bool appSetting;
        if (!bool.TryParse(ConfigurationManager.AppSettings["UseCustomModule"], 
                           out appSetting))
            appSetting = false;

        return appSetting;
    }

ASP.NET会看到它,对 UseCustomModule 我们的当前位置的适当的值是一个我们看。

ASP.NET will see to it that the appropriate value of UseCustomModule for our current location is the one we read.

这篇关于Asp.net的HttpModule目录级别的Web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆