我在Stack Overflow上问过这个问题,但也许这是一个经验丰富的IIS 7pipe理员可能更多的了解,所以我也在这里问。
使用Microsoft.Web.Administration命名空间操作Handler Mappings时,是否有一种方法可以删除在站点级别添加的<remove name="handler name">标记。
例如,我有一个从全局处理器映射configurationinheritance所有处理器映射的站点。 在applicationHost.config , <location>标签看起来像这样:
<location path="60030 - testsite-60030.com"> <system.webServer> <security> <authentication> <anonymousAuthentication userName="" /> </authentication> </security> </system.webServer> </location>
要删除处理程序,我使用类似这样的代码:
string siteName = "60030 - testsite-60030.com"; string handlerToRemove = "ASPClassic"; using(ServerManager sm = new ServerManager()) { Configuration siteConfig = serverManager.GetApplicationHostConfiguration(); ConfigurationSection handlersSection = siteConfig.GetSection("system.webServer/handlers", siteName); ConfigurationElementCollection handlersCollection = handlersSection.GetCollection(); ConfigurationElement handlerElement = handlersCollection .Where(h => h["name"].Equals(handlerMapping.Name)).Single(); handlersCollection.Remove(handlerElement); }
等效的APPCMD指令将是:
appcmd set config "60030 - autotest-60030.com" -section:system.webServer/handlers /-[name='ASPClassic'] /commit:apphost
这导致网站的<location>标签看起来像:
<location path="60030 - testsite-60030.com"> <system.webServer> <security> <authentication> <anonymousAuthentication userName="" /> </authentication> </security> <handlers> <remove name="ASPClassic" /> </handlers> </system.webServer> </location>
到现在为止还挺好。 但是,如果我重新添加ASPClassic处理程序,这将导致:
<location path="60030 - testsite-60030.com"> <system.webServer> <security> <authentication> <anonymousAuthentication userName="" /> </authentication> </security> <handlers> <!-- Why doesn't <remove> get removed instead of tacking on an <add> directive? --> <remove name="ASPClassic" /> <add name="ASPClassic" path="*.asp" verb="GET,HEAD,POST" modules="IsapiModule" scriptProcessor="%windir%\system32\inetsrv\asp.dll" resourceType="File" /> </handlers> </system.webServer> </location>
使用Microsoft.Web.Administration命名空间和C#或使用以下APPCMD命令时会发生这种情况:
appcmd set config "60030 - autotest-60030.com" -section:system.webServer/handlers /+[name='ASPClassic',path='*.asp',verb=;'GET,HEAD,POST',modules='IsapiModule',scriptProcessor='%windir%\system32\inetsrv\asp.dll',resourceType='File'] /commit:apphost
这可能会导致每个网站有一个处理程序被删除,然后重新添加编程的时间很多cruft。 有没有办法使用Microsoft.Web.Administration命名空间代码或APPCMD删除<remove name="ASPClassic" />标记?
如果您在特定站点/应用程序/ virdir上手动添加或删除处理程序映射/模块,则会添加<remove />标记。 现在,如果你再次手动添加, <remove />标签将不会去,因为对于IIS来说,这意味着你在configuration上做了一些改变。 通过这种方式,您可以灵活地添加您自己的自定义映射或模块来覆盖全局模块/映射。
是的,这会产生很多混乱,但这是它被处理的方式..至less现在。 处理这种情况的一种方法是在“动作”面板中单击“还原为父级”。
我也问了Stack Overflow,因为主题横跨两个站点。 这是我得到的答案:
如何在IIS7中使用Microsoft.Web.Administration命名空间干净地处理Handler映射?