2013-05-07 97 views
5

我試圖自動化IIS應用程序創建,併爲此我使用Microsoft.Web.Administration庫。下面是部分代碼我使用:通過使用Microsoft.Web.Administration庫創建IIS應用程序創建兩個應用程序而不是一個

IISHelper.cs

public static void CreateApplicationPool(string applicationPoolName) 
{ 
    using (ServerManager serverManager = new ServerManager()) 
    { 
     if (serverManager.ApplicationPools[applicationPoolName] != null) 
      return; 
     ApplicationPool newPool = serverManager.ApplicationPools.Add(applicationPoolName); 
     newPool.ManagedRuntimeVersion = "v4.0"; 
     serverManager.CommitChanges(); 
    } 
} 

public static void CreateSite(string siteName, string path) 
{ 
    using (ServerManager serverManager = new ServerManager()) 
    { 
     var sites = serverManager.Sites; 
     if (sites[siteName] == null) 
     { 
      sites.Add(siteName, "http", "*:80:", path); 
      serverManager.CommitChanges(); 
     } 
    } 
} 

public static void CreateApplication(string siteName, string applicationName, string path) 
{ 
    using (ServerManager serverManager = new ServerManager()) 
    { 
     var site = GetSite(serverManager, siteName); 
     var applications = site.Applications; 
     if (applications["/" + applicationName] == null) 
     { 
      applications.Add("/" + applicationName, path); 
      serverManager.CommitChanges(); 
     } 
    } 
} 

public static void CreateVirtualDirectory(string siteName, string applicationName, string virtualDirectoryName, string path) 
{ 
    using (ServerManager serverManager = new ServerManager()) 
    { 
     var application = GetApplication(serverManager, siteName, applicationName); 
     application.VirtualDirectories.Add("/" + virtualDirectoryName, path); 
     serverManager.CommitChanges(); 
    } 
} 

public static void SetApplicationApplicationPool(string siteName, string applicationPoolName) 
{ 
    using (ServerManager serverManager = new ServerManager()) 
    { 
     var site = GetSite(serverManager, siteName); 
     if (site != null) 
     { 
      foreach (Application app in site.Applications) 
      { 
       app.ApplicationPoolName = applicationPoolName; 
      } 
     }     
     serverManager.CommitChanges(); 
    } 
} 

這裏是它要求從上述IISHelper類

string siteName = "TestSite"; 
string applicationName = "TestApp"; 
string virtualDirectoryName = "TestVirtualDirectory"; 
string applicationPoolName = "TestAppPool"; 
string physicalPath = @"C:\inetpub\mynewsite"; 

IISHelper.CreateApplicationPool(applicationPoolName); 
IISHelper.CreateSite(siteName, physicalPath); 
IISHelper.CreateApplication(siteName, applicationName, physicalPath); 
IISHelper.CreateVirtualDirectory(siteName, applicationName, virtualDirectoryName, physicalPath); 
IISHelper.SetApplicationApplicationPool(siteName, applicationPoolName); 

方法的這個執行後的主代碼代碼我得到了成功創建的應用程序與IIS下的網站和適當的應用程序池,但是當我在新創建的應用程序池的菜單中選擇「查看應用程序」時,我看到有兩個應用程序,但我是sur e必須只有一個應用程序。我正在努力避免創建兩個應用程序,但找不到方法。這裏是一個圖片說明是:

enter image description here

因此,這裏是我的問題:爲什麼經過上述代碼執行兩個應用程序的創建,以及如何修改代碼以避免這個問題?

+2

GetSite&GetApplication在哪裏引用? – thestar 2014-07-29 16:05:06

+0

public static Site GetSite(ServerManager serverManager,string siteName) { return serverManager.Sites.FirstOrDefault(x => x.Name == siteName); } 私有靜態應用GetApplication( ServerManager的ServerManager的, 串SITENAME, 串的applicationName) { 返回serverManager.Sites .FirstOrDefault(X => x.Name == SITENAME)?應用 .FirstOrDefault(X => x.Path ==「/」+ applicationName); } – 2017-08-10 03:29:45

回答

2

每個網站都必須包含一個「Root Application」,這就是您所看到的第二個應用程序。基本上,當您在上面請求您的網站爲'http://example.com/'(或您在綁定中設置的任何主機名稱)時,您將從該根應用程序請求,此外您正在創建/ TestApp,在調用'http://example.com/TestApp'時將會請求該應用程序。當您創建站點並提供物理路徑時,它會在內部爲其創建一個根應用「/」。 要看到這個,你可以打開%windir%\ system32 \ inetsrv \ applicationHost.config。

因此,技術上你所看到的是正確的,但你可能不需要一個新的應用程序?只需要root應用程序?你真的需要/ TestApp在URL中嗎?