0
(0)

This post helps you to create a new website in IIS.
It Includes
– Create new website
– Setup binding information like port and IP address
– Create Application Pool
– Remove a website from the IIS

Pre-Requisite

  • Add Microsoft.web.Administration dll reference to the project library. you can find this library file in  system32\inetsrv folder.
  • Add the following line to your web config

         <identity impersonate=”true” userName=”adminname” password=”adminpwd”/>

Create an Object with the name of IIS Object

public class IISObject

   {

   public long siteId { get; set; }

   public string hostName { get; set; }

   public string webSiteName { get; set; }

   public string portNumber { get; set; }

   public string applicationPool { get; set; }      

    }

Below is the code to add website binding to a Host 

public void AddBindings(string sitename,string hostname)

  {    

  ServerManager serverMgr = new ServerManager();

  Site mySite = serverMgr.Sites[sitename];

  mySite.Bindings.Add("*:80:"+hostname, "http");

   // Set auto start to true.

  mySite.ServerAutoStart = true;

  // Commit the changes

  serverMgr.CommitChanges();

}

Below is the code to add the website to IIS

void AddWebSite( IISObject iisObject)

 {  

   ServerManager serverMgr = new ServerManager();                     

   string bindinginfo = "*:" + ConfigurationManager.AppSettings["portnumber"] + ":" + iisObject.hostName;

    //check if website name already exists in IIS

   Boolean bWebsite = IsWebsiteExists(iisObject.webSiteName, serverMgr);

    if (!bWebsite)

  {

   //ConfigurationManager.AppSettings["websitefolder"]  is the physical path of the directory

   Site mySite = serverMgr.Sites.Add(iisObject.webSiteName, "http", bindinginfo,ConfigurationManager.AppSettings["websitefolder"]);

   // name of the application pool, normally can use DefaultAppPool

   mySite.ApplicationDefaults.ApplicationPoolName =ConfigurationManager.AppSettings["applicationpool"];

 mySite.TraceFailedRequestsLogging.Enabled = true;

  string logginDirectory = ConfigurationManager.AppSettings["loggingdirectory"];

   mySite.TraceFailedRequestsLogging.Directory = logginDirectory + "\\" + iisObject.webSiteName;

   serverMgr.CommitChanges();

   } 

  }

Remove Binding from the Host Name

public  void RemoveServerBinding(string hostname,string sitename)

  {

  ServerManager serverMgr = new ServerManager();

    Site mySite = serverMgr.Sites[sitename];

   for (int i = 0; i < mySite.Bindings.Count; i++)

   {              

    if (mySite.Bindings[i].Host == hostname)

   {

    mySite.Bindings.RemoveAt(i);

   break;

  }

 } 

   mySite.ServerAutoStart = true; 

   }

Remove Website from IIS

public void RemoveIISWebsite(string sitename)

   { 

   ServerManager serverMgr = new ServerManager();

   Site site = serverMgr.Sites[sitename]; // you can pass the site name or the site ID

   //check site exists or not

   bool blnSuccess = false;

   blnSuccess = IsWebsiteExists(sitename, serverMgr);

   if (blnSuccess)

   {

   serverMgr.Sites.Remove(site);

   serverMgr.CommitChanges();              

   }          

   }

 

//Get all the website name and IDs

 

 public List<IISObject> GetIISSite()

  {

  //created an instance for pet object

  List<IISObject> iisSiteList = new List<IISObject>();

  ServerManager serverMgr = new ServerManager();

  SiteCollection sitecollection = serverMgr.Sites;

  foreach (var site in sitecollection)

  {

  IISObject issObject = new IISObject() { webSiteName = site.Name,siteId = site.Id };

  iisSiteList.Add(issObject);

  }

 

  return iisSiteList;

  }

 

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Share this post on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?