Monday, 25 March 2013

programmatically Publish Website using

This article describes how to create a new web site and publish web site programmatically on IIS using ASP.NET and C#.
On the server side, web service does the job. It takes the website name as parameter and creates new website on IIS. And a point to be noted is all the new websites created point to a same physical location and depending upon the name of the website, content gets populated. If you need to configure different physical paths for each website, send an additional parameter to the web service

Add Reference to “System.DirectoryServices” library and navigate to .cs file and import the namespaces “using System.DirectoryServices”.

//before add reference .dll file

 public bool PublishWebSite(string SiteName, string filepath, string portNumber, string IpAdress)
                string siteName = SiteName;
                string applicationPoolName = "ASP.NET v4.0 Classic";
                string virtualDirectoryPath = "/";
                //Path to the folder of the published code
                string virtualDirectoryPhysicalPath = filepath;
                //IP address of current machine where the site is to be published
                string ipAddress = IpAdress;
                //Port to be assigned to the site
                string tcpPort = portNumber;
                //Site name that appears in the URL
                string hostHeader = "";//string.Format("www.{0}", siteName);
                string applicationPath = "/";
                long highestId = 1;
                using (ServerManager serverManager = new ServerManager())
                    Site site = serverManager.Sites[siteName];
                    if (site != null)
                        return false;
                    ApplicationPool appPool = serverManager.ApplicationPools[applicationPoolName];
                    if (appPool == null)
                        throw new Exception(String.Format("Application Pool: {0} does not exist.", applicationPoolName));
                    highestId = serverManager.Sites.Max(i => i.Id);
                    site = serverManager.Sites.CreateElement();
                    site.SetAttributeValue("name", siteName);
                    site.Id = highestId;
                    //Assign the IP address , Port and site URL
                    string bind = ipAddress + ":" + tcpPort + ":" + hostHeader;
                    Binding binding = site.Bindings.CreateElement();
                    binding.Protocol = "http";
                    binding.BindingInformation = bind;
                    Application application = site.Applications.CreateElement();
                    application.Path = applicationPath;
                    application.ApplicationPoolName = applicationPoolName;
                    //Create a virtual directory in IIS
                    VirtualDirectory vdir = application.VirtualDirectories.CreateElement();
                    vdir.Path = virtualDirectoryPath;
                    //Set the physical path of the folder
                    vdir.PhysicalPath = virtualDirectoryPhysicalPath;
                    //Add this new site to the pool of sites in IIS
                return true;
            catch (Exception ex)
                return false;

No comments:

Post a Comment