Programmatically create a web site in IIS using C# and set port number

前端 未结 5 961
春和景丽
春和景丽 2020-11-28 04:16

We have been able to create a web site. We did this using the information in this link:

https://msdn.microsoft.com/en-us/library/ms525598.aspx

However, we wo

5条回答
  •  孤独总比滥情好
    2020-11-28 04:43

    Heres the solution.
    Blog article : How to add new website in IIS 7

    On Button click :

    try
     {
      ServerManager serverMgr = new ServerManager();
      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:\\inetpub\\wwwroot\\yourWebsite");
            mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool;
            mySite.TraceFailedRequestsLogging.Enabled = true;
            mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site";
            serverMgr.CommitChanges();
            lblmsg.Text = "New website  " + strWebsitename + " added sucessfully";
         }
         else
         {
            lblmsg.Text = "Name should be unique, " + strWebsitename + "  is already exists. ";
         }
       }
      catch (Exception ae)
      {
          Response.Redirect(ae.Message);
       }
    

    Looping over sites whether name already exists

        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;
        }
    

提交回复
热议问题