Using ASP. NET to manipulate applications in IIS7

  • 2021-07-02 23:53:23
  • OfStack

In the latest release of Venus Portal, an installation program has been added, which is described below. NET operates on IIS7. The operation of IIS7 is quite different from that of IIS5/6. In IIS7, Microsoft. Web. Administration namespace is added, and ServerManager and Site are added to operate IIS7.

The following are some core codes, which can be used directly

Create a virtual directory

When creating a virtual directory, "Default Web Site" is used by default, that is, Default Web Site and CreateVdir need 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;  
} 

What is established here is a virtual directory under Default Web Site, and the above mysite is modified to


Site mySite = iisManager.Sites.Add("test", "http", "*:80:" + WebName + ".intranet." + TLD, @"c:\Webs\" + WebName); 

You can create a Web site. The two differences are: You set up a website. The previous access gesture URL is http://www.dotnetcms.org/book, while 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 value of ManagedPipelineMode is ManagedPipelineMode. Classic; IIS7 supports classic Classic mode and Interget integrated mode

Custom handler and Module may not work. If you want to be compatible with the previous version of IIS 5/6, you can use Classic, otherwise, it is recommended to use integrated mode.

The following code demonstrates how to assign virtual directories to application pools. The biggest difference from IIS5/6 is that vdir is actually vdir path, so a "/" is added here to indicate 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 1 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; 
}  
} 

At this point, the basic functions of NET operation IIS7 have been realized, hoping to help everyone's study.


Related articles: