Microsoft.web.administrationネームスペースを使用して、IIS7でハンドラーマッピングをきれいに操作するにはどうすればよいですか?
質問
を使用してハンドラーマッピングを操作する場合 Microsoft.Web.Administration
名前空間、削除する方法はありますか <remove name="handler name">
サイトレベルで。
たとえば、グローバルハンドラーマッピング構成からすべてのハンドラーマッピングを継承するサイトがあります。の 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>
これにより、ハンドラーが削除されてからプログラムで再添加された各Webサイトの時間の経過とともに、多くのCruftをもたらす可能性があります。ただ削除する方法はありますか <remove name="ASPClassic" />
Microsoft.Web.Administration Namespaceコードを使用していますか?
解決
これについてはIIS製品チームと話し合いましたが、これは構成システムのバグのようです。さらに興味深いのは、IIS 7.5を使用してWin7でこのコードを試してみると、プログラムでハンドラーを再添加することさえできないということです。そうしようとすると、次のようなCOM例外が発生します。
「エラー:「Aspclassic」に設定された一意のキー属性「名前」を使用して、「追加」タイプ「追加」の重複コレクションエントリを追加できません」
ユーザーが場所のハンドラーを「削除」すると、このバグが修正されるまでMWA APIを介して再入れることができないため、さらに問題が発生します。