[英]Get IIS Web Site Application Name
I'm trying to get the web application name I'm currently in. (Where my application code is deployed in IIS).我正在尝试获取我当前所在的 Web 应用程序名称。(我的应用程序代码部署在 IIS 中的位置)。
I can get the IIS server name:我可以获得 IIS 服务器名称:
string IISserverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
The current web site:当前网站:
string currentWebSiteName = HostingEnvironment.ApplicationHost.GetSiteName();
I can't find a way to get the web application name!我找不到获取 Web 应用程序名称的方法! Because I need to build a path, depending in what web application am I, to get all virtual directories.
因为我需要根据我是什么 Web 应用程序构建一个路径来获取所有虚拟目录。
The Oct 23 answer only iterates through all the apps. 10 月 23 日的答案仅遍历所有应用程序。 The question was how to obtain the CURRENT application name from an application running on IIS.
问题是如何从运行在 IIS 上的应用程序获取当前应用程序名称。 Ironically, the question above helped me answer it.
具有讽刺意味的是,上面的问题帮助我回答了它。
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
Add the following reference to your application: "c:\\windows\\system32\\inetsrv\\Microsoft.web.Administration.dll"将以下引用添加到您的应用程序:“c:\\windows\\system32\\inetsrv\\Microsoft.web.Administration.dll”
and use the code below to enumerate web site names and appropriate application names.并使用下面的代码枚举网站名称和适当的应用程序名称。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.