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

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

使用 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 异常,该异常指出:

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

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

关于c# - 如何使用 Microsoft.Web.Administration 命名空间在 IIS7 中干净地操作处理程序映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1671702/

相关文章:

c# - 如何将 IQueryable<string> 转换为字符串?

asp.net-mvc - 通过代码请求客户端证书

javascript - 领英分享文章。网址错误

c# - 如何以编程方式限制对 Web 服务的访问?

c# - WSHttpBinding TransportWithMessageCredential SecurityMode 改变请求

c# - ILogger.Log 方法 declaringType 参数

c# - Telerik Ajax UpdatePanel radgrid 等待页面加载

c# - 以标准方式访问没有值(value)的短信 Web 服务中的子 XML 值

c# - 为什么在使用对象池并尝试将对象放在地形上的随机位置时它不是随机的?

c# - 在使用时强制重命名文件夹