爲了呼應Marvin Smit's comment,似乎一個在web.config
<location>
下配置<modules>
根本不起作用 - 以這種方式指定的任何模塊不被調用。
什麼你可以做的是指定在根級的模塊,並使其通過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>
爲CustomModule
然後,代碼內:
private static bool ModuleEnabled()
{
bool appSetting;
if (!bool.TryParse(ConfigurationManager.AppSettings["UseCustomModule"],
out appSetting))
appSetting = false;
return appSetting;
}
ASP.NET會看到它的UseCustomModule
我們當前位置的合適值時,我們讀到了一個。
我當然不喜歡這是答案的事實,但感謝確認我的懷疑。 – EricTheRed 2014-01-04 20:46:59
@AakashM:一些觀察1)ModuleEnabled()方法需要調用某處,我試着在Init中調用它,如果返回值爲false,則無法從那裏繼續工作2)當對ModuleEnabled的調用處於事件例如PreSendRequestHeaders,它的作品。總而言之,Module被初始化一次,然後對於每次調用,該標誌被檢查。如果你看到任何差異,請糾正我。 – dotnetguy 2014-01-21 14:59:24