【发布时间】:2020-10-30 02:20:37
【问题描述】:
我们推荐stackOverflow 和其他一些站点使用控制台应用程序在 IIS 上创建网站。在我们以管理权限运行控制台后,它就起作用了。代码确实在给定端口上创建了一个应用程序池和主机站点。当我们在 asp.net Web 应用程序上尝试相同的代码时,它完成了执行,但我们没有在 IIS 上找到元数据。
这是在控制台上运行的代码..
try
{
ServerManager server = new ServerManager();
ApplicationPool myApplicationPool = null;
//we will first check to make sure that this pool does not already exist
//since the ApplicationPools property is a collection, we can use the Linq FirstOrDefault method
//to check for its existence by name
if (server.ApplicationPools != null && server.ApplicationPools.Count > 0)
{
if (server.ApplicationPools.FirstOrDefault(p => p.Name == "TestPool") == null)
{
//if the pool is not already there we will create it
myApplicationPool = server.ApplicationPools.Add("TestPool");
}
else
{
//if we find the pool already there, we will get a referecne to it for update
myApplicationPool = server.ApplicationPools.FirstOrDefault(p => p.Name == "TestPool");
}
}
else
{
//if the pool is not already there we will create it
myApplicationPool = server.ApplicationPools.Add("TestPool");
}
if (myApplicationPool != null)
{
//for this sample, we will set the pool to run under the NetworkService identity
myApplicationPool.ProcessModel.IdentityType =
ProcessModelIdentityType.NetworkService;
//for this sample, we will set the pool to run under the identity of a specific user
//myApplicationPool.ProcessModel.IdentityType =
ProcessModelIdentityType.SpecificUser;
//myApplicationPool.ProcessModel.UserName = UserName;
//myApplicationPool.ProcessModel.Password = Password;
//we set the runtime version
myApplicationPool.ManagedRuntimeVersion = "v4.0";
//we save our new ApplicationPool!
server.CommitChanges();
}
//Create website
if (server.Sites != null && server.Sites.Count > 0)
{
//we will first check to make sure that the site isn't already there
if (server.Sites.FirstOrDefault(s => s.Name == "MySite") == null)
{
//we will just pick an arbitrary location for the site
string path = @"C:\inetpub\Custom";
//we must specify the Binding information
string ip = "*";
string port = "98";
string hostName = "*";
string bindingInfo = string.Format(@"{0}:{1}:{2}", ip, port, hostName);
//add the new Site to the Sites collection
Site site = server.Sites.Add("MySite", "http", bindingInfo, path);
//set the ApplicationPool for the new Site
site.ApplicationDefaults.ApplicationPoolName = myApplicationPool.Name;
//save the new Site!
server.CommitChanges();
Console.WriteLine("Web site created successfully...");
Console.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine()
}
现在,当在 Web 应用程序上尝试相同的代码时,它不会在 IIS 上创建任何内容。当我们检查服务器管理器对象时,我们发现应用程序池列表来自项目的
applicationhost.config
位于 .vs 隐藏文件夹中的文件。
我们在本地机器上安装了最新的 IIS 以进行测试,是否需要进行任何更改才能使其在 Web 上也能正常工作。 (:我们是 IIS 的新手)
【问题讨论】:
-
很可能,您的 ASP.NET Web 应用程序 ApplicationPool 用户没有足够的权限来执行必要的操作。检查服务器的应用程序事件日志是否有错误。
-
也没有错误,当我们重建并逐行检查时,我们确实找到了我们之前输入的最后一个应用程序池名称,表示代码创建了应用程序池,但我们不知道它在哪里创建以及如何访问它
-
您可能添加了对 Microsoft.Web.Administration 的错误引用,blog.lextudio.com/… 此外,请卸载 IIS Express,因为它始终是此类情况的垫脚石。
-
是的,@LexLi,也许吧!现在必须通过它