【问题标题】:How do I manipulate Handler Mappings cleanly in IIS7 using the Microsoft.Web.Administration namespace?如何使用 Microsoft.Web.Administration 命名空间在 IIS7 中干净地操作处理程序映射?
【发布时间】:2009-11-04 04:46:32
【问题描述】:

当使用 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);
}

这导致网站的&lt;location&gt; 标记看起来像:

<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 命名空间代码删除 &lt;remove name="ASPClassic" /&gt;

【问题讨论】:

    标签: c# .net iis iis-7 iis-7.5


    【解决方案1】:

    我已与 IIS 产品团队讨论过这个问题,这似乎是配置系统的一个错误。更有趣的是,当我在带有 IIS 7.5 的 Win7 上尝试此代码时,我什至无法以编程方式重新添加处理程序。尝试这样做会导致 COM 异常,指出:

    “错误:无法添加类型为 'add' 且唯一键属性 'name' 设置为 'ASPClassic' 的重复集合条目”

    这变得更加成问题,因为一旦用户“删除”了某个位置的处理程序,就无法通过 M.W.A. 重新添加它。 API 直到这个 bug 被修复。

    【讨论】:

    • 感谢托宾的回复,现在我知道我不是唯一对此挑剔的人。值得在 Microsoft Connect 上打开问题吗?
    • 是的。我会说继续打开它。团队已经意识到这一点,但值得使用 Connect 进行跟踪。
    • @Kev,嗨,我也在尝试做一些类似的事情,尝试使用 WMI 从 IIS7 中的网站中删除处理程序映射。该代码似乎工作正常,但从未删除处理程序映射。正如我在删除后看到的inetmgr。请检查:stackoverflow.com/questions/7102056/…
    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多