如何使用 Microsoft.Web.Administration 命名空间在 IIS7 中干净地操作处理程序映射?

如何使用 Microsoft.Web.Administration 命名空间在 IIS7 中干净地操作处理程序映射?

我在 Stack Overflow 上问过这个问题,但也许经验丰富的 IIS 7 管理员可能对此了解更多,所以我也在这里问。

当使用命名空间操作处理程序映射时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);
}

等效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

随着时间的推移,这会导致每个网站都出现大量垃圾,因为这些网站删除了处理程序,然后又通过编程重新添加。有没有办法只<remove name="ASPClassic" />使用 Microsoft.Web.Administration 命名空间代码删除标签APPCMD

答案1

如果您在特定站点/应用程序/virdir 上手动添加或删除处理程序映射/模块,您将获得<remove />添加的标记。现在,如果您再次手动添加它,标记<remove />将不会消失,因为对于 IIS 来说,这意味着您在配置中做了一些更改。这样,您就可以灵活地添加自己的自定义映射或模块,以覆盖全局模块/映射。

是的,这会产生很多混乱,但这就是处理方式……至少目前是这样。处理此问题的一种方法是单击“操作”面板中的“恢复为父级”。

答案2

我也在 Stack Overflow 上问过这个问题,因为这个主题在两个网站上都有。这是我得到的答案:

如何使用 Microsoft.Web.Administration 命名空间在 IIS7 中干净地操作处理程序映射?

相关内容