Add an ISAPI Filter to an existing Site with Microsoft.Web.Administration

谁说胖子不能爱 提交于 2019-12-12 13:14:53

问题


I'm trying to add/configure an ISAPI Filter through c# and the Microsoft.Web.Administration assembly. So far I didn't manage to add an an ISAPI Filter for a single Web Site.

I just found this Article (http://www.iis.net/ConfigReference/system.webServer/isapiFilters) for adding it in the global setting for the whole IIS. I just need it for a specific site. I'm using IIS 7.5.


回答1:


You just need to tweak the example given (see the inline comments):

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();

If you don't know the site name but know the site ID (or IIS number) then you can query the name by doing:

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


来源:https://stackoverflow.com/questions/8729879/add-an-isapi-filter-to-an-existing-site-with-microsoft-web-administration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!