Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

port

iis

iis-6

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 would like to use a port number other that port 80. How do we do this?

We are using IIS 6

like image 854
Shiraz Bhaiji Avatar asked Aug 17 '09 08:08

Shiraz Bhaiji


People also ask

Can we develop website using C#?

NET and C# Use . NET and C# to create websites based on HTML5, CSS, and JavaScript that are secure, fast, and can scale to millions of users.

How do I create a new website in IIS?

Go to Control Panel > Administrative Tools > Internet Information Services (IIS) Manager. In the Connections panel, expand your host tree, right-click on Sites, and choose Add Website. Enter the new website's name and choose the location. Enter the Host name.


2 Answers

If you're using IIS 7, there is a new managed API called Microsoft.Web.Administration

An example from the above blog post:

ServerManager iisManager = new ServerManager(); iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite"); iisManager.CommitChanges();  

If you're using IIS 6 and want to do this, it's more complex unfortunately.

You will have to create a web service on every server, a web service that handles the creation of a website because direct user impersonation over the network won't work properly (If I recall this correctly).

You will have to use Interop Services and do something similar to this (This example uses two objects, server and site, which are instances of custom classes that store a server's and site's configuration):

string metabasePath = "IIS://" + server.ComputerName + "/W3SVC"; DirectoryEntry w3svc = new DirectoryEntry(metabasePath, server.Username, server.Password);  string serverBindings = ":80:" + site.HostName; string homeDirectory = server.WWWRootPath + "\\" + site.FolderName;   object[] newSite = new object[] { site.Name, new object[] { serverBindings }, homeDirectory };  object websiteId = (object)w3svc.Invoke("CreateNewSite", newSite);  // Returns the Website ID from the Metabase int id = (int)websiteId; 

See more here

like image 78
kitsune Avatar answered Sep 19 '22 11:09

kitsune


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;     } 
like image 45
satinder singh Avatar answered Sep 20 '22 11:09

satinder singh