使用Microsoft.Web.Administration将ISAPI筛选器添加到现有站点

时间:2012-01-04 16:02:51

标签: c# .net iis-7.5

我正在尝试通过c#和Microsoft.Web.Administration程序集添加/配置ISAPI筛选器。到目前为止,我没有设法为单个网站添加ISAPI筛选器。

我刚刚发现this Article (http://www.iis.net/ConfigReference/system.webServer/isapiFilters)将其添加到整个IIS的全局设置中。我只需要一个特定的网站。我正在使用IIS 7.5。

1 个答案:

答案 0 :(得分:2)

您只需要调整给出的示例(请参阅内联注释):

ServerManager serverManager = new ServerManager();   

Configuration config = serverManager.GetApplicationHostConfiguration();

// Change this line:    
ConfigurationSection isapiFiltersSection = 
                           config.GetSection("system.webServer/isapiFilters");

// To this by adding an extra param specifying the site name:
ConfigurationSection isapiFiltersSection = 
              config.GetSection("system.webServer/isapiFilters", "my site name");


ConfigurationElementCollection isapiFiltersCollection = 
                           isapiFiltersSection.GetCollection();

ConfigurationElement filterElement = 
                        isapiFiltersCollection.CreateElement("filter");
filterElement["name"] = @"SalesQueryIsapi";
filterElement["path"] = @"c:\Inetpub\www.contoso.com\filters\SalesQueryIsapi.dll";
filterElement["enabled"] = true;
filterElement["enableCache"] = true;
isapiFiltersCollection.Add(filterElement);

serverManager.CommitChanges();

如果您不知道站点名称但知道站点ID(或IIS号码),那么您可以通过执行以下操作来查询名称:

int iisNumber = 12345;
string siteName = serverManager.Sites.Single(s => s.Id == iisNumber).Name;