简体   繁体   中英

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

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM