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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10919295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:45:33  来源:igfitidea点击:

Get IIS Web Site Application Name

c#.netiis

提问by vinhent

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! Because I need to build a path, depending in what web application am I, to get all virtual directories.

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

采纳答案by user2514070

The Oct 23 answer only iterates through all the apps. The question was how to obtain the CURRENT application name from an application running on IIS. Ironically, the question above helped me answer it.

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

回答by Hovo

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);
    }
}