如何使用C#动态地在IIS上添加站点?

时间:2014-07-09 07:54:13

标签: c# asp.net iis

我正在ASP.NET中开发一个多商店Web应用程序,可以在其中创建具有不同域的多个商店。我有一个向导来创建一个商店来填充信息。我想要什么,当我克隆向导的完成按钮,我想在IIS上添加特定的商店网站。我怎么能用C#实际做到这一点。

private void ConfigureSiteInIis()
{
     string strWebsitename = txtStoreName.Text; // abc
     const string strApplicationPool = "DefaultAppPool"; // set your deafultpool :4.0 in IIS
     string strhostname = txtDomainName.Text; //abc.com
     const string stripaddress = "localhost:40411"; // ip address
     string bindinginfo = stripaddress + ":80:" + strhostname;
     var serverMgr=new ServerManager();

     //Site mySite = serverMgr.Sites.Add(txtStoreName.Text, "http", bindinginfo, "C:\\inetpub\\wwwroot\\yourWebsite");
     Site mySite = serverMgr.Sites.Add(txtStoreName.Text, "http", "*:80:" + strhostname , Server.InetPath+txtDomainName.Text);
     mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool;
     mySite.TraceFailedRequestsLogging.Enabled = true;
     mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site";
     serverMgr.CommitChanges();
     lblMessage.Text = "New website  " + strWebsitename + " added sucessfully";
     lblMessage.ForeColor = System.Drawing.Color.Green;
}

我从stackoverflow获得了这个代码,但它抛出了异常"指定的HTTPS绑定无效"。

1 个答案:

答案 0 :(得分:1)

我之前写过这篇文章,完美无缺(IIS7及更高版本):

public static Application CreateApplicaiton(string siteName, string path, string physicalPath, string appPoolName)
    {
        ServerManager iisManager = ServerManager.OpenRemote(Environment.MachineName.ToLower());

        // should start with "/" also not to end with this symbol
        string correctApplicationPath = string.Format("{0}{1}", Path.AltDirectorySeparatorChar, path.Trim(Path.AltDirectorySeparatorChar));

        int indexOfApplication = correctApplicationPath.LastIndexOf(Path.AltDirectorySeparatorChar);
        if (indexOfApplication > 0)
        {
            // create sequence of virtual directories if the path is not a root level (i.e. test/beta1/Customer1/myApplication)
            string virtualDirectoryPath = correctApplicationPath.Substring(0, indexOfApplication);
            iisManager.CreateVirtualDirectory(siteName, virtualDirectoryPath, string.Empty);
        }

        Application application = iisManager.Sites[siteName].Applications.Add(correctApplicationPath, physicalPath);
        application.ApplicationPoolName = appPoolName;

        return application;
    }

localhost/test/beta1/Customer1/myApplication托管申请,请使用以下参数:

siteName - 您在IIS中的网站名称| Default Web Site

path - 虚拟目录| test/beta1/Customer1/myApplication

    public static void CreateVirtualDirectory(this ServerManager iisManager, string siteName, string path, string physicalPath)
    {
        Site site = iisManager.Sites[siteName];

        //remove '/' at the beginning and at the end
        List<string> pathElements = path.Trim(Path.AltDirectorySeparatorChar).Split(Path.AltDirectorySeparatorChar).ToList();

        string currentPath = string.Empty;
        List<string> directoryPath = pathElements;

        //go through applications hierarchy and find the deepest one
        Application application = site.Applications.First(a => a.Path == Path.AltDirectorySeparatorChar.ToString());
        for (int i = 0; i < pathElements.Count; i++)
        {
            string pathElement = pathElements[i];
            currentPath = string.Join(Path.AltDirectorySeparatorChar.ToString(), currentPath, pathElement);

            if (site.Applications[currentPath] != null)
            {
                application = site.Applications[currentPath];
                if (i != pathElements.Count - 1)
                {
                    directoryPath = pathElements.GetRange(i + 1, pathElements.Count - i - 1);
                }
            }
        }

        currentPath = string.Empty;
        foreach (string pathElement in directoryPath)
        {
            currentPath = string.Join(Path.AltDirectorySeparatorChar.ToString(), currentPath, pathElement);

            //add virtual directories
            if (application.VirtualDirectories[currentPath] == null)
            {
                //assign physical path of application root folder by default
                string currentPhysicalPath = Path.Combine(application.VirtualDirectories[0].PhysicalPath, pathElement);

                //if this is last element of path, use physicalPath specified on method call
                if (pathElement == pathElements.Last() && !string.IsNullOrWhiteSpace(physicalPath))
                {
                    currentPhysicalPath = physicalPath;
                }

                currentPhysicalPath = Environment.ExpandEnvironmentVariables(currentPhysicalPath);

                if (!Directory.Exists(currentPhysicalPath))
                {
                    Directory.CreateDirectory(currentPhysicalPath);
                }

                application.VirtualDirectories.Add(currentPath, currentPhysicalPath);
            }
        }
    }