Many Web installers add applications or application pools to IIS, and early operation of IIS with ASP is difficult, but starting with 7.0, Microsoft provides the Microsoft.Web.Administration class, which makes it easy to operate IIS.
This article mainly introduces four points:
I. Adding an application
Two. Add an application pool
Three. Set the application pool used by the application
Four. Settings for other properties in IIS
First, you must make sure that IIS is installed on your computer, and after installation, the system registers a DLL by default, usually in the location
C:\Windows\assembly\GAC_MSIL\Microsoft.Web.Administration\7.0.0.0__31bf3856ad364e35\ Microsoft.Web.Administration.dll
To get sites and applications, the code is:
public static ApplicationPool Getapppool () { Servermanager manager = new Servermanager (); Return manager. applicationpools; } public static SiteCollection Getallsite () { Servermanager manager = new Servermanager (); SiteCollection sites = Manager. Sites; return sites; }
I. Adding an application
Add the application manually, usually on "Default Web Site", right-click, select "Add Application ..." and in the popup dialog, enter the application name and physical path as shown in.
This operation, using C # code to manipulate as
public static void Createvdir (string vdir, String phydir) { Servermanager servermanager = new Servermanager (); Site mySite = servermanager.sites["Default Web Site"]; mySite.Applications.Add ("/" + VDir, Phydir); Servermanager.commitchanges (); }
You first define a Servermanager object and then use sites to get a collection of sites. , and finally call the Add method in the Applications property to append the application to the site. However, please note: the establishment of the application is "/" for the path, so, the above code in the parameters passed in, directly enter the application name.
To establish an application named Test under the default Web site defaults site, whose physical path is D:\web\sample, is called
Createvdir ("Test", "D:\\web\\sample")
Deleting an application is also straightforward, just call the Remove method.
public static void Deletevdir (String vdirname) { Servermanager servermanager = new Servermanager (); Site mySite = servermanager.sites["Default Web Site"]; Microsoft.Web.Administration.Application application = mysite.applications["/" + vdirname]; mySite.Applications.Remove (application); Servermanager.commitchanges (); }
Second, add the application pool
Adding application pools Manually in IIS is added directly to the IIS Control Panel.
Use C # code as follows
public static void Createapppool (String apppoolname) { Servermanager servermanager = new Servermanager (); SERVERMANAGER.APPLICATIONPOOLS.ADD (apppoolname); ApplicationPool AppPool = servermanager.applicationpools[apppoolname]; AppPool. Managedpipelinemode = managedpipelinemode.integrated; AppPool. Enable32bitAppOnWin64 = true; AppPool. Managedruntimeversion = "v4.0"; Servermanager.commitchanges (); AppPool. Recycle (); }
From the above code can be seen, ApplicationPool provides Enable32bitAppOnWin64, Managedpipelinemode, Managedruntimeversion and other properties, can be easily used.
Just like the code above, you can set AppPool directly using Integrated mode. Managedpipelinemode = managedpipelinemode.integrated.
Removing an application pool is also relatively straightforward, calling the Remove method
public static void Deleteapppool (String apppoolname) { Servermanager servermanager = new Servermanager (); ApplicationPool AppPool = Servermanager.applicationpools[apppoolname]; ServerManager.ApplicationPools.Remove (AppPool); Servermanager.commitchanges (); }
Three: Set the application pool used by the application
Manual operation: In IIS, after selecting an application, in the advanced settings, you can directly modify the application pool used by the application.
To do something like the above, you can set the Applicationpoolname property of applications, which represents the application pool name used by the application.
Public static void Assignvdirtoapppool (String vdir, String apppoolname) { Servermanager Servermanager = New Servermanager (); Site site = servermanager.sites["Default Web site"]; site. applications["/" + VDir]. Applicationpoolname = AppPoolName; Servermanager.commitchanges (); }
Four: Control other properties in IIS
In addition to application and application pools, using the Servermanager class can also manipulate more properties in IIS. For example, set restrictions for ISAPI and CGI.
Manual action: Open ISAPI and CGI operations, as required. NET version is enabled or disabled.
Complete the above code as follows
using (Servermanager Servermanager = new Servermanager ()) {Configuration conf IG = Servermanager.getapplicationhostconfiguration (); ConfigurationSection isapicgirestrictionsection = config. GetSection ("System.webserver/security/isapicgirestriction"); ConfigurationElementCollection isapicgirestrictioncollection = Isapicgirestrictionsection.getcollection (); foreach (configurationelement element in isapicgirestrictioncollection) {string Path = element["path"]. ToString (); string allowed = element["allowed"]. ToString (); String description = element["description"]. ToString (); String groupId = element["GroupId"]. ToString ();if (Description = = "ASP. v4.0.30319") { element["allowed"] = true; }} servermanager.commitchanges (); }
You can even add your own configuration to the Ispapi:
ConfigurationElement addelement = isapicgirestrictioncollection.createelement ("add"); addelement["path"] = @ "C:\Windows\abc\aspnet_isapi.dll"; addelement["allowed"] = true; addelement["groupId"] = @ "Contosogroup"; addelement["description"] = @ "Contoso Extension"; Isapicgirestrictioncollection.add (addelement); Servermanager.commitchanges ();
Of course, the "difficulty" in the above code is that you may not know what the section ("System.webserver/security/isapicgirestriction") of each attribute is in IIS, but don't worry
Microsoft's official documentation lists each node in detail, and you can do so simply by using the appropriate nodes as needed.
The website is: https://docs.microsoft.com/en-us/iis/configuration/system.webserver/security/isapicgirestriction/
With the properties above, you can use these classes to control IIS if you later want to customize the EXE version of the Web installer.
Working with ASP. IIS (You can make the Setup program)