Programmatically Create IIS Website and Application Pool Using C#

In this article, we will look into creation of an IIS 7\7.5 website and corresponding application pool programmatically using a C# console application. These activities are most commonly done by an IIS administrator. We can automate those activities using this application. Let's create a console application and name it IISAutomation in Visual Studio 2010 by targeting .NET 3.5 as in the following:

console application

Let's add a reference to Microsoft.Web.Administration that is present under C:\Windows\System32\inetsrv [IIS installation directory]. This assembly contains classes that a developer can use to administer the IIS Manager. First we will create an application pool using the following code:

  1. private static void CreateAppPool(string poolname,bool enable32bitOn64, ManagedPipelineMode mode,string runtimeVersion="v4.0")  
  2. {  
  3.     using (ServerManager serverManager = new ServerManager())  
  4.     {  
  5.         ApplicationPool newPool = serverManager.ApplicationPools.Add(poolname);  
  6.         newPool.ManagedRuntimeVersion = runtimeVersion;  
  7.         newPool.Enable32BitAppOnWin64 = true;  
  8.         newPool.ManagedPipelineMode = mode;  
  9.         serverManager.CommitChanges();  
  10.     }  
  11. }  
Here, we created an application pool by creating an instance of ApplicationPool object. Then, we set the properties of the application pool, like the name, .NET version to be used and the committed changes by calling CommitChanges(). Similarly, we will create a website using the following code:
  1. private static void CreateIISWebsite(string websiteName, string hostname, string phyPath, string appPool)  
  2. {  
  3.     ServerManager iisManager = new ServerManager();  
  4.     iisManager.Sites.Add(websiteName, "http""*:80:" + hostname, phyPath);  
  5.     iisManager.Sites[websiteName].ApplicationDefaults.ApplicationPoolName = appPool;  
  6.   
  7.     foreach (var item in iisManager.Sites[websiteName].Applications)  
  8.     {  
  9.         item.ApplicationPoolName = appPool;  
  10.     }  
  11.   
  12.     iisManager.CommitChanges();  
  13. }  
Here, we created a new web site by creating an instance of ServerManager, adding it to the Sites collection and setting its properties like name, physical path and so on and committed the changes.

We will use the preceding methods in our Main method and create an application pool and a web site using the following code:
  1. static void Main(string[] args)  
  2. {  
  3.     Console.WriteLine("Do you want to create an Application Pool:y/n");  
  4.     string response = Console.ReadLine();  
  5.     if (response.ToString() == "y")  
  6.     {  
  7.         Console.Write("Please enter Application Pool Name:");  
  8.         string poolname = Console.ReadLine();  
  9.         bool isEnable32bit = false;  
  10.         ManagedPipelineMode mode = ManagedPipelineMode.Classic;  
  11.         Console.Write("Need to enable 32 bit on Windows 64 bit?y/n [Applicable for 64 bit OS]: y/n?");  
  12.         string enable32bit = Console.ReadLine();  
  13.         if (enable32bit.ToLower() == "y")  
  14.         {  
  15.             isEnable32bit = true;  
  16.         }  
  17.         Console.Write("Please select Pipeline Mode: 1 for Classic, 2 for Integrated:");  
  18.         string pipelinemode = Console.ReadLine();  
  19.         if (pipelinemode.ToLower() == "2")  
  20.         {  
  21.             mode = ManagedPipelineMode.Integrated;  
  22.         }  
  23.         Console.Write("Please select Runtime Version for Application Pool: 1 for v2.0, 2 for v4.0:");  
  24.         string runtimeVersion = Console.ReadLine()== "1" ? "v2.0" : "v4.0";  
  25.           
  26.         CreateAppPool(poolname, isEnable32bit, mode, runtimeVersion);  
  27.         Console.WriteLine("Application Pool created successfully...");  
  28.     }  
  29.                 Console.WriteLine("Do you want to create a website:y/n");  
  30.     response = Console.ReadLine();  
  31.     if (response.ToString() == "y")  
  32.     {  
  33.         Console.Write("Please enter website name:");  
  34.         string websiteName = Console.ReadLine();  
  35.         Console.Write("Please enter host name:");  
  36.         string hostname = Console.ReadLine();  
  37.         Console.Write("Please enter physical path to point for website:");  
  38.         string phypath = Console.ReadLine();  
  39.         Console.WriteLine("Please enter Application pool Name:");  
  40.         foreach(var pool in new ServerManager().ApplicationPools)  
  41.         {  
  42.             Console.WriteLine(pool.Name);  
  43.         }  
  44.         Console.WriteLine("");  
  45.         Console.Write("Please enter Application pool Name for web site:");  
  46.         string poolName = Console.ReadLine();  
  47.         CreateIISWebsite(websiteName,hostname,phypath,poolName);  
  48.         Console.WriteLine("Web site created successfully...");  
  49.         Console.ReadLine();  
  50.     }  
  51. }  
Here, we set the attributes necessary for web site and application creation by getting input from the console. Let's run the application and input the details as shown below:

run the application

By using this application, we can create web sites and application pools faster and easier as well. I am ending up the things here. I am attaching the source code for reference. I hope this article will be helpful for all.

Up Next
    Ebook Download
    View all
    Learn
    View all