2012-06-06 86 views
3

我正在嘗試獲取當前所在的Web應用程序名稱(其中我的應用程序代碼部署在IIS中)。獲取IIS網站應用程序名稱C#.Net

我可以得到IIS服務器名稱:

string IISserverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"]; 

當前網站:

string currentWebSiteName = HostingEnvironment.ApplicationHost.GetSiteName(); 

我不能找到一個辦法讓Web應用程序的名字!因爲我需要建立一個路徑,取決於我的Web應用程序,以獲取所有虛擬目錄。

+0

不明白Web應用程序名稱將如何幫助您獲取所有虛擬目錄 –

+0

虛擬目錄正在進入Web應用程序。需要這個名字來構建我的路徑。 – Vinhent

+0

仍然不明白,對不起。但究竟是應用程序名稱?應用程序池名稱? IIS站點名稱?或什麼 –

回答

11

10月23日答案只遍歷所有的應用程序。問題是如何從運行在IIS上的應用程序獲取CURRENT應用程序名稱。諷刺的是,上面的問題幫助我回答了這個問題。

using Microsoft.Web.Administration; 
using System.Web.Hosting; 

ServerManager mgr = new ServerManager(); 
string SiteName = HostingEnvironment.ApplicationHost.GetSiteName(); 
Site currentSite = mgr.Sites[SiteName]; 

//The following obtains the application name and application object 
//The application alias is just the application name with the "/" in front 

string ApplicationAlias = HostingEnvironment.ApplicationVirtualPath; 
string ApplicationName = ApplicationAlias.Substring(1); 
Application app = currentSite.Applications[ApplicationAlias]; 

//And if you need the app pool name, just use app.ApplicationPoolName 
+4

'ApplicationHost.GetSiteName()'不打算在代碼中使用(請參閱https://msdn.microsoft.com/en-us/庫/ system.web.hosting.iapplicationhost.getsitename(v = vs.110)的.aspx)。而是使用'System.Web.Hosting.HostingEnvironment.SiteName;'。 – EvilDr

1

添加以下引用您的應用程序:「C:\ WINDOWS \ SYSTEM32 \ INETSRV \ Microsoft.web.Administration.dll」

,並使用下面的代碼來枚舉網站的名稱和相應的應用程序名稱。

using Microsoft.Web.Administration; 

//.. 

var serverManager = new ServerManager(); 
foreach (var site in serverManager.Sites) 
{ 
    Console.WriteLine("Site: {0}", site.Name); 
    foreach (var app in site.Applications) 
    { 
     Console.WriteLine(app.Path); 
    } 
} 
+0

ServerManager也有「ApplicationPools」和「ApplicationDefaults.ApplicationPoolName」這是很酷 –

相關問題