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

Overview: Here I am writing an article for Creating website in IIS programmatically using c#.For Adding or deleting a website from IIS here, is 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 convenience access to server objects.

Intro:

IIS includes Microsoft.Web.Administration, which is a new a management API for the web server 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 through these objects managing the server is seamless.

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 a sites properties and applications. 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.apsx.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.

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

Sharing is Sexy:    
Satinder Singh

Hi all, am Founder and Author of Codepedia.info, also atypical polyglot programmer from Bombay, India. I have a passion to create, solve, and deploy software applications.
       ForEach ( minute in MyLife ) { myExperience++ ; }


5 comments on “How to add new website into IIS Programmatically using Asp.net C#?”


Post Comment


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