获取 IIS 网站应用程序名称

本文关键字:应用程序 网站 IIS 获取 | 更新日期: 2023-09-27 18:36:22

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

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

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

当前网站:

string currentWebSiteName = HostingEnvironment.ApplicationHost.GetSiteName();

我找不到获取 Web 应用程序名称的方法!因为我需要构建一个路径,具体取决于我是什么 Web 应用程序,以获取所有虚拟目录。

获取 IIS 网站应用程序名称

10 月 23 日的答案仅遍历所有应用程序。 问题是如何从 IIS 上运行的应用程序获取当前应用程序名称。 具有讽刺意味的是,上面的问题帮助我回答了这个问题。

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

向应用程序添加以下引用:"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);
    }
}