积极答复者
如何编写IIS模块或配置IIS模块(Module)

问题
-
需求是这样的:
在IIS的配置文件(applicationHost.config)里,有这样的代码:
<section name="globalModules" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
这段代码禁止了在自有的config(web.config)里编写image的可能:
<globalModules> <add name="UriCacheModule" image="%windir%\System32\inetsrv\cachuri.dll" /> <add name="FileCacheModule" image="%windir%\System32\inetsrv\cachfile.dll" /> <add name="TokenCacheModule" image="%windir%\System32\inetsrv\cachtokn.dll" /> <add name="HttpCacheModule" image="%windir%\System32\inetsrv\cachhttp.dll" /> <add name="StaticCompressionModule" image="%windir%\System32\inetsrv\compstat.dll" /> <add name="DefaultDocumentModule" image="%windir%\System32\inetsrv\defdoc.dll" /> <add name="DirectoryListingModule" image="%windir%\System32\inetsrv\dirlist.dll" /> <add name="ProtocolSupportModule" image="%windir%\System32\inetsrv\protsup.dll" /> <add name="StaticFileModule" image="%windir%\System32\inetsrv\static.dll" /> <add name="AnonymousAuthenticationModule" image="%windir%\System32\inetsrv\authanon.dll" /> <add name="RequestFilteringModule" image="%windir%\System32\inetsrv\modrqflt.dll" /> <add name="CustomErrorModule" image="%windir%\System32\inetsrv\custerr.dll" /> <add name="HttpLoggingModule" image="%windir%\System32\inetsrv\loghttp.dll" /> <add name="RequestMonitorModule" image="%windir%\System32\inetsrv\iisreqs.dll" /> <add name="IsapiModule" image="%windir%\System32\inetsrv\isapi.dll" /> <add name="IsapiFilterModule" image="%windir%\System32\inetsrv\filter.dll" /> <add name="ManagedEngine64" image="%windir%\Microsoft.NET\Framework64\v2.0.50727\webengine.dll" preCondition="integratedMode,runtimeVersionv2.0,bitness64" /> <add name="ManagedEngine" image="%windir%\Microsoft.NET\Framework\v2.0.50727\webengine.dll" preCondition="integratedMode,runtimeVersionv2.0,bitness32" /> <add name="ConfigurationValidationModule" image="%windir%\System32\inetsrv\validcfg.dll" /> <add name="iisnode" image="C:\Program Files\iisnode\iisnode.dll" preCondition="bitness64" /> <add name="ManagedEngineV4.0_32bit" image="%windir%\Microsoft.NET\Framework\v4.0.30319\webengine4.dll" preCondition="integratedMode,runtimeVersionv4.0,bitness32" /> <add name="ManagedEngineV4.0_64bit" image="%windir%\Microsoft.NET\Framework64\v4.0.30319\webengine4.dll" preCondition="integratedMode,runtimeVersionv4.0,bitness64" /> </globalModules>
因此,当我在web.config节点system.webServer.httpModules里编写上述类似代码的时候报告了禁止的错误。
于是,自行编写了一个实现IHttpModule的module,放置在web.config里:
<modules runAllManagedModulesForAllRequests="false"> <remove name="FormsAuthentication" /> <add name="DirectoryToFileModule" type="Zupo.Lifenxiang.UI.DirectoryModule, Zupo.Lifenxiang.UI.Core" /> </modules>
然后再:
<add name="staticfile" path="*" verb="*" resourceType="Either" requireAccess="Read" modules="StaticFileModule,DefaultDocumentModule,DiretoryToFileModule" preCondition="" />
系统也报告错误,说配置不正确,我知道这个错误信息是说我写的module不可用。
我的实际情况是:
1、文件存储在仓库里(比如数据库里)
2、当我访问指定文件夹下的路径时,如果目标文件(静态文件)存在,则执行系统的静态文件处理程序。
3、如果访问的目标是一个文件夹,此时,如果文件夹下存在指定的默认文件,比如__default__.jpg,则直接使用这个文件
4、当访问的是目录,而目录里有其它文件(如abc.jpg)但默认文件不存在,此时,按照IIS策略是要执行文件列表功能的。
4.1、我要的就是拦截这个文件列表功能,自己写程序控制,自动将已有文件abc.jpg复制为默认文件,然后输出默认文件
4.2、如果文件夹里没有任何默认文件,则通过自定义的列目录模块实现目标文件(比如默认文件)的生成(调用数据库处理,或重定向到其它url实现生成)
5、当访问的目录不存在或目标文件不存在时,启动4.2的步骤,生成文件,配置如下:
<add name="nostaticfile" path="*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
发现问题:
1、4这个步骤没办法控制
2、5这个也不稳定,有时能执行,有时又执行不了。
3、系统不支持staticfile和nostaticfile节点的重复配置。
答案
-
你好:
可以参考一下微软官方的教程,希望对你有帮助:
How to Create a Simple IIS Manager Module
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 Will ShaoMicrosoft employee, Moderator 2015年3月30日 6:27