2009-11-04 62 views
3

使用Microsoft.Web.Administration名稱空間處理處理程序映射時,是否有辦法在站點級別刪除<remove name="handler name">如何在IIS7中使用Microsoft.Web.Administration命名空間乾淨地處理Handler映射?

例如,我有一個站點從全局處理程序映射配置繼承所有處理程序映射。在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); 
} 

這導致該網站的<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> 
     <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命名空間代碼刪除<remove name="ASPClassic" />

回答

2

我已經與IIS產品團隊討論過這個問題,這似乎是配置系統的一個錯誤。更有趣的是,當我在Win7上使用IIS 7.5嘗試此代碼時,我甚至無法以編程方式重新添加處理程序。試圖這樣做的結果,指出一個COM異常:

「錯誤:無法添加類型的重複的集合項‘添加’具有獨特的關鍵屬性‘名稱’設置爲‘ASPClassic’」

變得更加因爲一旦用戶已經「移除」某個位置的處理程序,就不能通過MWA重新添加它API,直到這個bug被修復。

+0

感謝Tobin的迴應,現在我知道我不是唯一對此感到挑剔的人。是否值得在Microsoft Connect上打開一個問題? – Kev 2009-11-06 03:01:17

+0

是的。我會說,繼續打開它。該團隊已經意識到這一點,但將Connect用於追蹤非常值得。 – tobint 2009-11-06 08:24:19

+0

@Kev, 嗨,我也試圖做一些類似的事情,試圖從使用WMI的IIS7中的網站中刪除處理程序映射。該代碼似乎工作正常,但處理程序映射從不刪除。因爲我可以在移除後看到它的inetmgr。請檢查此:http://stackoverflow.com/questions/7102056/how-to-remove-handler-mapping-from-a-website-in-iis7-using-wmi-and-c-vb-net – 2011-08-18 03:31:11