自定义HttpHandler时,有时候怎么配置都不起效,这里记录下:
自定义完HttpHandler或HttpModule后需要再web.config中进行注册。
注册HttpHandler在节点 <system.webServer>下:
<handlers> <remove name="WebServiceHandlerFactory-Integrated"/> <remove name="ScriptHandlerFactory"/> <remove name="ScriptHandlerFactoryAppServices"/> <remove name="ScriptResource"/> <!--自定义handler--> <add name="pngFileHandler" path="File/*.png" verb="*" type="TestDownloadIntercept.MyHandler.MyFileHandler,TestDownloadIntercept"/> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </handlers>
注册HttpModule,在节点<system.webServer>下面:
<modules> <remove name="ScriptModule"/> <add name="MyFileModule" type="TestDownloadIntercept.MyModule.MyFileModule,TestDownloadIntercept"/> <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </modules>
在webconfig注册时,可以写在第一行,不知道是不是这个原因,我注册时,很长时间不起效。