Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Asp.net web C在IIS上动态创建网站#_C#_Asp.net_Iis - Fatal编程技术网

C# 使用Asp.net web C在IIS上动态创建网站#

C# 使用Asp.net web C在IIS上动态创建网站#,c#,asp.net,iis,C#,Asp.net,Iis,我们参考了其他一些网站,使用控制台应用程序在IIS上创建了一个网站。在我们以管理权限运行控制台之后,它就工作了。代码在给定端口上创建应用程序池和主机站点。当我们在asp.net web应用程序上尝试时,它完成了执行,但在IIS上找不到元数据 下面是一段在控制台上运行的代码 try { ServerManager server = new ServerManager(); ApplicationPool myApplicationPool = null;

我们参考了其他一些网站,使用控制台应用程序在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()
}
在这里,该站点也被列在IIS中。

现在,当在web应用程序上尝试相同的代码时,它不会在IIS上创建任何内容。当我们检查服务器管理器对象时,我们发现应用程序池列表来自项目的

applicationhost.config

位于.vs隐藏文件夹中的文件


我们在本地机器上安装了最新的IIS进行测试,是否需要进行任何更改才能使其在web上正常工作。(:我们是IIS新手)

您可以尝试以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;

namespace IISTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                Console.WriteLine("Do you want to create an Application Pool:y/n");
                string response = Console.ReadLine();
                if (response.ToString() == "y")
                {
                    Console.Write("Please enter Application Pool Name:");
                    string poolname = Console.ReadLine();
                    bool isEnable32bit = false;
                    ManagedPipelineMode mode = ManagedPipelineMode.Classic;
                    Console.Write("Need to enable 32 bit on Windows 64 bit?y/n [Applicable for 64 bit OS]: y/n?");
                    string enable32bit = Console.ReadLine();
                    if (enable32bit.ToLower() == "y")
                    {
                        isEnable32bit = true;
                    }
                    Console.Write("Please select Pipeline Mode: 1 for Classic, 2 for Integrated:");
                    string pipelinemode = Console.ReadLine();
                    if (pipelinemode.ToLower() == "2")
                    {
                        mode = ManagedPipelineMode.Integrated;
                    }
                    Console.Write("Please select Runtime Version for Application Pool: 1 for v2.0, 2 for v4.0:");
                    string runtimeVersion = Console.ReadLine() == "1" ? "v2.0" : "v4.0";

                    CreateAppPool(poolname, isEnable32bit, mode, runtimeVersion);
                    Console.WriteLine("Application Pool created successfully...");
                }
                Console.WriteLine("Do you want to create a website:y/n");
                response = Console.ReadLine();
                if (response.ToString() == "y")
                {
                    Console.Write("Please enter website name:");
                    string websiteName = Console.ReadLine();
                    Console.Write("Please enter host name:");
                    string hostname = Console.ReadLine();
                    Console.Write("Please enter physical path to point for website:");
                    string phypath = Console.ReadLine();
                    Console.WriteLine("Application pool Name:");
                    foreach (var pool in new ServerManager().ApplicationPools)
                    {
                        Console.WriteLine(pool.Name);
                    }
                    Console.WriteLine("");
                    Console.Write("Please enter Application pool Name for web site:");
                    string poolName = Console.ReadLine();
                    CreateIISWebsite(websiteName, hostname, phypath, poolName);
                    Console.WriteLine("Web site created successfully...");
                    Console.ReadLine();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
        private static void CreateIISWebsite(string websiteName, string hostname, string phyPath, string appPool)
        {
            ServerManager iisManager = new ServerManager();
            iisManager.Sites.Add(websiteName, "http", "*:80:" + hostname, phyPath);
            iisManager.Sites[websiteName].ApplicationDefaults.ApplicationPoolName = appPool;

            foreach (var item in iisManager.Sites[websiteName].Applications)
            {
                item.ApplicationPoolName = appPool;
            }

            iisManager.CommitChanges();
        }
        private static void CreateAppPool(string poolname,bool enable32bitOn64, ManagedPipelineMode mode,string runtimeVersion="v4.0")
        {
            using (ServerManager serverManager = new ServerManager())
            {
                ApplicationPool newPool = serverManager.ApplicationPools.Add(poolname);
                newPool.ManagedRuntimeVersion = runtimeVersion;
                newPool.Enable32BitAppOnWin64 = true;
                newPool.ManagedPipelineMode = mode;
                serverManager.CommitChanges();
            }
        }
    }
}

注意:不要忘记添加对Microsoft.Web.Administration的引用。

很可能是您的ASP.NET Web应用程序应用程序池用户没有足够的权限执行必要的操作。检查服务器的应用程序事件日志是否有错误。也没有错误,当我们逐行重建和检查时,我们确实找到了我们之前输入的最后一个应用程序池名称,这意味着代码创建了应用程序池,但是我们不知道它是在哪里创建的,也不知道如何访问它。您可能在Microsoft.Web.Administration中添加了错误的引用,此外,请卸载IIS Express,因为在这种情况下,它始终是一块踏脚石。是的,@LexLi,可能!我也试过这个,同样的结果代码完成了,但对我的工作没有影响IIS@T.Giri您是否尝试以管理员身份运行visual studio?是的,我尝试以管理员身份运行studio,并尝试授予对的完全访问权限user@T.Giri尝试在iis中部署应用程序并对其进行测试