在 system.webServer 元素內(nèi),創(chuàng)建一個(gè) modules 元素;在 modules 元素內(nèi)創(chuàng)建一個(gè) add 元素,并在 name 和 type 屬性中指定自定義模塊。實(shí)際的名稱(chēng)和類(lèi)型取決于要添加的模塊;
下面的示例演示如何添加名為CustomModule的自定義模塊,該模塊將實(shí)現(xiàn)為類(lèi)型Samples.CustomModule。
<add name="CustomModule" type="Samples.CustomModule" />
向模塊注冊(cè)中添加 precondition 屬性,并將其值設(shè)置為managedHandler。
此前置條件會(huì)導(dǎo)致僅在請(qǐng)求 ASP.NET 應(yīng)用程序資源(例如 .aspx 文件或托管處理程序)時(shí)才調(diào)用該模塊。該資源中不包括靜態(tài)文件(例如 .htm 文件)。
其 configuration 節(jié)將類(lèi)似于以下示例。
<add name="CustomModule" type="Samples.CustomModule" precondition="managedHandler" /><add value="Products.aspx" />
關(guān)于MVC
對(duì)于像MVC這種比較特殊的URL,例如www.store.com/books/GetById/2
因?yàn)闆](méi)有文件后綴名,IIS通常會(huì)無(wú)法解析,返回403或者404錯(cuò)誤。
ASP.NET v4.0增加了新的特性,當(dāng)運(yùn)行在IIS7以上版本,并且需要IIS的一個(gè)快速修復(fù)程序KB980368,配置web.config后,將會(huì)正常處理上面這種 extensionless URL。
IIS經(jīng)典模式下的配置
服務(wù)器會(huì)繼續(xù)通過(guò) Aspnet_isapi.dll 路由托管代碼請(qǐng)求,其處理請(qǐng)求的方式就像應(yīng)用程序在 IIS 6.0 中運(yùn)行一樣,aspnet_isapi.dll(IIS的native handler擴(kuò)展),通過(guò)映射到System.Web.DefaultHttpHandler進(jìn)行處理。

<validation validateIntegratedModeConfiguration="false" /><modules runAllManagedModulesForAllRequests="true" /><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.NETFramework4.0.30319spnet_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.NETFramework644.0.30319spnet_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" />

IIS集成模式下的配置
服務(wù)器將使用 IIS 和 ASP.NET 的集成請(qǐng)求處理管道來(lái)處理請(qǐng)求,映射到System.Web.Handlers.TransferRequestHandle來(lái)處理
<modules runAllManagedModulesForAllRequests="true" /><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" />
經(jīng)典模式(classic mode)和集成模式(Integrated mode)比較
在經(jīng)典模式下,IIS會(huì)用ISAPI擴(kuò)展(ISAPI extension aspnet_isapi.dll)和 ISAPI過(guò)濾器(ISAPI filter aspnet_filter.dll)來(lái)調(diào)用ASP.NET運(yùn)行庫(kù)來(lái)處理請(qǐng)求。
使用經(jīng)典模式的話,服務(wù)器會(huì)用兩種管道來(lái)處理請(qǐng)求一個(gè)負(fù)責(zé)源代碼,另外一個(gè)負(fù)責(zé)托管代碼。在這種模式下,應(yīng)用程序不能充分使用IIS7.X提供的服務(wù)。
httpHandler 和 httpModule 在集成和經(jīng)典模式下的區(qū)別
經(jīng)典模式,在web.config的
集成模式,在web.config的