Programmatically Create IIS Website and Application Pool Using C#

Introduction

In this article, we will look into the 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:

private static void CreateAppPool(string pool name,bool enable32bitOn64, ManagedPipelineMode mode,string runtimeVersion="v4.0")  
{  
    using (ServerManager serverManager = new ServerManager())  
    {  
        ApplicationPool newPool = serverManager.ApplicationPools.Add(poolname);  
        newPool.ManagedRuntimeVersion = runtimeVersion;  
        newPool.Enable32BitAppOnWin64 = true;  
        newPool.ManagedPipelineMode = mode;  
        serverManager.CommitChanges();  
    }  
}  

Here, we created an application pool by creating an instance of the application pool object. Then, we set the properties of the application pool, like the name, the .NET version to be used, and the committed changes by calling CommitChanges(). Similarly, we will create a website using the following code:

private static void CreateIISWebsite(string website name, string hostname, string phyPath, string app pool)  
{  
    ServerManager iisManager = new ServerManager();  
    iisManager.Sites.Add(websiteName, "http", "*:80:" + hostname, phyPath);  
    iisManager.Sites[websiteName].ApplicationDefaults.ApplicationPoolName = appPool;  
  
    foreach (var item in iisManager.Sites[websiteName].Applications)  
    {  
        item.ApplicationPoolName = appPool;  
    }  
  
    iisManager.CommitChanges();  
}  

Here, we created a new website by creating an instance of ServerManager, adding it to the Sites collection 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 website using the following code:

static void Main(string[] args)  
{  
    Console.WriteLine("Do you want to create an Application Pool:y/n");  
    string response = Console.ReadLine();  
    if (response.ToString() == "y")  
    {  
        Console.Write("Please enter Application Pool Name:");  
        string poolname = Console.ReadLine();  
        bool isEnable32bit = false;  
        ManagedPipelineMode mode = ManagedPipelineMode.Classic;  
        Console.Write("Need to enable 32 bit on Windows 64 bit?y/n [Applicable for 64 bit OS]: y/n?");  
        string enable32bit = Console.ReadLine();  
        if (enable32bit.ToLower() == "y")  
        {  
            isEnable32bit = true;  
        }  
        Console.Write("Please select Pipeline Mode: 1 for Classic, 2 for Integrated:");  
        string pipelinemode = Console.ReadLine();  
        if (pipelinemode.ToLower() == "2")  
        {  
            mode = ManagedPipelineMode.Integrated;  
        }  
        Console.Write("Please select Runtime Version for Application Pool: 1 for v2.0, 2 for v4.0:");  
        string runtimeVersion = Console.ReadLine()== "1" ? "v2.0" : "v4.0";  
          
        CreateAppPool(poolname, isEnable32bit, mode, runtimeVersion);  
        Console.WriteLine("Application Pool created successfully...");  
    }  
                Console.WriteLine("Do you want to create a website:y/n");  
    response = Console.ReadLine();  
    if (response.ToString() == "y")  
    {  
        Console.Write("Please enter website name:");  
        string websiteName = Console.ReadLine();  
        Console.Write("Please enter host name:");  
        string hostname = Console.ReadLine();  
        Console.Write("Please enter physical path to point for website:");  
        string phypath = Console.ReadLine();  
        Console.WriteLine("Please enter Application pool Name:");  
        foreach(var pool in new ServerManager().ApplicationPools)  
        {  
            Console.WriteLine(pool.Name);  
        }  
        Console.WriteLine("");  
        Console.Write("Please enter Application pool Name for web site:");  
        string poolName = Console.ReadLine();  
        CreateIISWebsite(websiteName,hostname,phypath,poolName);  
        Console.WriteLine("Web site created successfully...");  
        Console.ReadLine();  
    }  
}  

Here, we set the attributes necessary for website 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 websites 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.


Similar Articles