在 system.webServer 元素內,建立一個 modules 元素;在 modules 元素內建立一個 add 元素,並在 name 和 type 屬性中指定自定義模塊。實際的名稱和類型取決於要添加的模塊;
下面的示例演示如何添加名爲CustomModule的自定義模塊,該模塊將實現爲類型Samples.CustomModule。
<configuration>
<system.webServer>
<modules> <add name="CustomModule" type="Samples.CustomModule" /> </modules>
</system.webServer>
</configuration>
向模塊註冊中添加 precondition 屬性,並將其值設置爲managedHandler。
此前置條件會致使僅在請求 ASP.NET 應用程序資源(例如 .aspx 文件或託管處理程序)時才調用該模塊。該資源中不包括靜態文件(例如 .htm 文件)。
其 configuration 節將相似於如下示例。
<configuration>
<system.webServer>
<modules>
<add name="CustomModule" type="Samples.CustomModule" precondition="managedHandler" />
</modules>
<defaultDocument>
<files>
<add value="Products.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
關於MVC
對於像MVC這種比較特殊的URL,例如www.store.com/books/GetById/2
由於沒有文件後綴名,IIS一般會沒法解析,返回403或者404錯誤。
ASP.NET v4.0增長了新的特性,當運行在IIS7以上版本,而且須要IIS的一個快速修復程序KB980368,配置web.config後,將會正常處理上面這種 extensionless URL。
<validation validateIntegratedModeConfiguration="false"/> 這個主要做用是設置不檢測 <system.web>中的配置 。
<modules runAllManagedModulesForAllRequests="true" /> 這裏當設置爲true的時候,全部的請求,包含靜態文件的請求和沒有文件擴展名的請求,都會通過自定義的HttpModule,其實就是爲了僞靜態。
IIS經典模式下的配置
服務器會繼續經過 Aspnet_isapi.dll 路由託管代碼請求,其處理請求的方式就像應用程序在 IIS 6.0 中運行同樣,aspnet_isapi.dll(IIS的native handler擴展),經過映射到System.Web.DefaultHttpHandler進行處理。
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
IIS集成模式下的配置
服務器將使用 IIS 和 ASP.NET 的集成請求處理管道來處理請求,映射到System.Web.Handlers.TransferRequestHandle來處理
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
經典模式(classic mode)和集成模式(Integrated mode)比較
在經典模式下,IIS會用ISAPI擴展(ISAPI extension aspnet_isapi.dll)和 ISAPI過濾器(ISAPI filter aspnet_filter.dll)來調用ASP.NET運行庫來處理請求。
使用經典模式的話,服務器會用兩種管道來處理請求一個負責源代碼,另一個負責託管代碼。在這種模式下,應用程序不能充分使用IIS7.X提供的服務。
httpHandler 和 httpModule 在集成和經典模式下的區別
經典模式,在web.config的 <system.web> 的子節點<httpModules> 加入<add name="MyHttpModule" type="WebApplication.MyHttpModule, WebApplication"/>
集成模式,在web.config的 <system.webServer> <modules> 加入<add name="MyHttpModule" type="WebApplication.MyHttpModule, WebApplication"/>