How to add new website into IIS Programmatically in Asp.net C#?

/ / 5 Comments

Overview: Here I am writing an article for Creating a website in IIS programmatically using c#.For Adding or deleting a website from IIS here, in this scenario, I must say IIS7 is the best friend for the developers.

Because administering websites through the code was made very simple by Microsoft.Web.Administration.dll which is present in the %windir%system32inetsrv folder.IS 7.0 and above provide a comprehensive managed-code management application programming interface (API) that allows complete manipulation of the XML configuration files and access to server objects conveniently.

Intro:

IIS includes Microsoft.Web.Administration is a new management API for the webserver that enables editing configuration through complete manipulation of the XML configuration files. It also provides convenience objects to manage the server, its properties, and state.

The configuration editing aspect of the API provides programmatic access to read and write configuration properties in the IIS configuration file hierarchy and specific configuration files.

The object management aspect of this API provides a series of top-level administration objects for the direct management of the server (i.e. sites, application pools, worker processes, etc).

Using ServerManager Namespace:

The ServerManager is the factory class that contains a set of server convenience objects to which properties and methods are available to use in a strongly-type way. It is the main entry point for managing the server.

Managing the server could have been done via other cumbersome routes (accessing raw configuration XML or calling state APIs), but managing the server is seamless through these objects.

The most common sets of objects are available to use via the server manager include applications, virtual directories, sites, worker processes, and application domains
//*
ServerManager serverManager = new ServerManager();
//*
 
 
The sites object enables access to the properties and applications of a site. It also contains methods to add a site to the system or get the total site count. The add method also defines the name of the site, the root virtual directory path, and the port number as an integer.
Note:
  • Add Microsoft.web.Administration DLL.
  • Environment: IIS 7

Step by Step code to add website in IIS programmatically as given below:

Code : Page Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Web.Administration;
using System.Drawing;

namespace AddWebsiteIIS7
{
public partial class _Default : System.Web.UI.Page
{
  ServerManager serverMgr = new ServerManager();
 
protected void Page_Load(object sender, EventArgs e)
{
	lblmsg.Text = "";
}

protected void btncreate_Click(object sender, EventArgs e)
{
	try
	{ 
		 string strWebsitename = txtwebsitename.Text; // abc
		 string strApplicationPool = "DefaultAppPool";  // set your deafultpool :4.0 in IIS
		 string strhostname = txthostname.Text; //abc.com
		 string stripaddress = txtipaddress.Text;// ip address
		 string bindinginfo = stripaddress + ":80:" + strhostname;

		 //check if website name already exists in IIS
		 Boolean bWebsite = IsWebsiteExists(strWebsitename);
		 if (!bWebsite)
		 {
			  Site mySite = serverMgr.Sites.Add(strWebsitename.ToString(), "http", bindinginfo, "C:inetpubwwwrootyourWebsite");
			  mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool;
			  mySite.TraceFailedRequestsLogging.Enabled = true;
			  mySite.TraceFailedRequestsLogging.Directory = "C:inetpubcustomfoldersite";
			  serverMgr.CommitChanges();
			  lblmsg.Text = "New website  " + strWebsitename + " added sucessfully";
			  lblmsg.ForeColor = System.Drawing.Color.Green;
		 }
		 else{
		  lblmsg.Text = "Name should be unique, " + strWebsitename + "  is already exists. ";
		  lblmsg.ForeColor = System.Drawing.Color.Red;
		 }
	 }
 	catch (Exception ae)
 	{
  		Response.Redirect(ae.Message);
 	}
 }

 public bool IsWebsiteExists(string strWebsitename)
 {
  Boolean flagset = false;
  SiteCollection sitecollection = serverMgr.Sites;
	  foreach (Site site in sitecollection)
	  {
		   if (site.Name == strWebsitename.ToString())
		   {
		  	  flagset = true;
		  	  break;
		   }
		  else{
		  	 flagset = false;
		   }
	  }
 	 return flagset;
 	}
}
}
  Conclusion: This article demonstrated one of the concepts to create a new website on IIS programmatically using ASP.NET and C#.

Note:
  • Make sure your redirection.config file has Read, Write full Permission.
  • It is just my experienced which is explained in the above article also it doesn’t include additional things like exception handling & web service security. Please feel to modify it according to your requirements.
Location: c: windowsSystem32 inetsrv config redirection.config;
You can also check these articles:
  1. Chart.js Asp.net: Create Pie chart with database Jquery Ajax C#.
  2. Chart.js Asp.net: Create Line chart with database Jquery Ajax C#.
  3. Generic Handler ashx file: Post send JSON data in Asp.net C#.
  4. jQuery Ajax JSON Example in Asp.net with sql database.

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

5 Comments

  1. Anonymous 02/04/2012 11:16:55
    from where i can get microsoft.web.administration dll
  2. Satinder singh 02/06/2012 06:16:27
    Microsoft.web.administration dll is their in ur system you can add
    heres the location
    C:WindowsSystem32inetsrvmicrosoft.web.administration dll
  3. BhaktaBarik 05/11/2012 11:16:57
    Good One
  4. Anonymous 02/11/2013 06:35:21
    keep up your good work ur site is awesome

    prasanas
  5. vineet 04/12/2013 13:31:33
    Thanks