1

下面有一些非常错误的东西,但我无法弄清楚是什么。虽然网站创建得像一个魅力,但应该与之关联的应用程序池根本没有创建。

public string Create(string sitename)
        {
            try
            {
                using (ServerManager serverMgr = new ServerManager())
                {
                    string strhostname = sitename + "." + domain;
                    string bindinginfo = ":80:" + strhostname;

                    if (!IsWebsiteExists(serverMgr.Sites, strhostname))
                    {
                        Site mySite = serverMgr.Sites.Add(strhostname, "http", bindinginfo, "C:\\admin\\" + domain);

                        ApplicationPool newPool = serverMgr.ApplicationPools.Add(strhostname);
                        newPool.ManagedRuntimeVersion = "v4.0";
                        newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;

                        serverMgr.CommitChanges();
                        return "Website  " + strhostname + " added sucessfully";
                    }

                    else
                    {
                        return "Name should be unique, " + strhostname + " already exists.";
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

我在这里做错了什么?

4

2 回答 2

1

我不希望 App Pool 名称中包含标点符号。将域添加为应用程序池名称的一部分有点不寻常 - 也许这就是来源。这里讨论了基本方法,以及appcmd在命令行上进行相同操作的语法 - 尝试在 cmd 行上创建您的应用程序池,以查看您的参数是否可以接受。

创建使用 .NET 4.0 的应用程序池

于 2016-01-23T22:23:53.657 回答
1

这里发生的情况是,当您创建站点时,它会自动分配给DefaultAppPool.

您需要做的是替换站点的 Application( /) 并将其指向您刚刚创建的应用程序池。

最简单的方法是首先清除新站点的Application集合,然后添加一个指向应用程序池的新根应用程序。

以您的代码片段,我将其更改为以下内容:

Site mySite = serverMgr.Sites.Add(strhostname, "http", bindinginfo, "C:\\admin\\" + domain);

// Clear Applications collection
mySite.Applications.Clear();

ApplicationPool newPool = serverMgr.ApplicationPools.Add(strhostname);
newPool.ManagedRuntimeVersion = "v4.0";
newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;

// Create new root app and specify new application pool
Application app = mySite.Applications.Add("/", "C:\\admin\\" + domain);
app.ApplicationPoolName = strhostname;

serverMgr.CommitChanges();
于 2016-01-24T07:46:22.257 回答