ASP tutorial. NET operations IIS7 using program methods in application
Microsoft.Web.Administration namespace, the addition of Servermanager, site several large classes to operate IIS7.
Here are some core codes that you can use directly
1) Establish virtual directory
When you set up a virtual directory, default Web site is used, or default Web site is established, Createvdir requires two parameters: virtual path name and actual physical path
public static bool Createvdir (String vdir, String phydir)
{
Servermanager Servermanager = new Servermanager ();
Site mySite = servermanager.sites["Default Web Site"];
MYSITE.APPLICATIONS.ADD ("/" + VDir, Phydir);
Servermanager.commitchanges ();
return true;
}
Here is a virtual directory under the default Web site, the above MySite modified to
Site mySite = IISMANAGER.SITES.ADD ("Test", "http", "*:80:" + WebName + ". Intranet." + TLD, @ "C:webs" + WebName);
You can create a Web site. These 2 differences are: you build a website. The previous access hint URL is Http://www.dotnetcms.org/book, and the latter is http://book.dotnetcms.org
Next Create the application pool
public static void Createapppool (String apppoolname)
{
Try
{
Servermanager Servermanager = new Servermanager ();
SERVERMANAGER.APPLICATIONPOOLS.ADD (AppPoolName);
ApplicationPool AppPool = Servermanager.applicationpools[apppoolname];
AppPool. Managedpipelinemode = Managedpipelinemode.classic;
Servermanager.commitchanges ();
AppPool. Recycle ();
}
Catch
{ }
}
Here the Managedpipelinemode value MANAGEDPIPELINEMODE.CLASSIC;IIS7 support the classical Classic method and the Interget integration way, under the integrated mode
Custom handler and module may not be valid, and if you want to use the classic method for compatibility with previous IIS5/6 versions, it is recommended that you use an integration approach.
The following code shows how to assign a virtual directory to an application pool, and IIS5/6 the biggest difference is that VDir is actually vdir path, so here's a "/" that represents a virtual path.
public static void Assignvdirtoapppool (String vdir, String apppoolname)
{
Try
{
Servermanager Servermanager = new Servermanager ();
Site site = servermanager.sites["Default Web site"];
Site. applications["/" + VDir]. Applicationpoolname = AppPoolName;
Servermanager.commitchanges ();
}
Catch
{
}
}
Finally add a delete operation
public static bool Deletevdir (string vdirname)
{
Try
{
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 ();
return true;
}
Catch
{
return false;
}
}
To this,. NET Operation IIS7