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

StackOverflow https://stackoverflow.com/questions/1671702

  •  13-09-2019
  •  | 
  •  

在操纵处理程序映射时使用 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>

这可能会导致每个网站被删除,然后通过编程重新添加的每个网站会导致大量巡回赛。有没有办法只删除 <remove name="ASPClassic" /> 使用Microsoft.web.administration名称空间代码?

有帮助吗?

解决方案

我已经与IIS产品团队讨论了这一点,这似乎是配置系统的错误。更有趣的是,当我使用IIS 7.5上的Win7上尝试此代码时,我什至无法以编程方式重新添加处理程序。试图这样做会导致com例外指出:

“错误:无法添加'添加'添加'添加'添加的重复集合条目,'添加'sute键属性'名称'设置为“ aspclassic'”

这变得更加有问题,因为一旦用户“删除”了一个位置处理程序,直到修复此错误之前,它就无法通过MWA API重新添加。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top