获取IIS网站应用程序名称C#.Net

Vin*_*ent 9 .net iis web

我正在尝试获取我当前所在的Web应用程序名称.(我的应用程序代码部署在IIS中).

我可以获取IIS服务器名称:

string IISserverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
Run Code Online (Sandbox Code Playgroud)

目前的网站:

string currentWebSiteName = HostingEnvironment.ApplicationHost.GetSiteName();
Run Code Online (Sandbox Code Playgroud)

我找不到获取Web应用程序名称的方法!因为我需要根据我的Web应用程序构建一个路径来获取所有虚拟目录.

Ale*_*ins 20

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
Run Code Online (Sandbox Code Playgroud)

  • `ApplicationHost.GetSiteName()`不适用于代码(请参阅https://msdn.microsoft.com/en-us/library/system.web.hosting.iapplicationhost.getsitename(v=vs.110). ASPX).而是使用`System.Web.Hosting.HostingEnvironment.SiteName;`. (7认同)

小智 5

将以下引用添加到您的应用程序:“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);
    }
}
Run Code Online (Sandbox Code Playgroud)