C# 从 ASP.NET 网站获取 IIS 站点名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1654797/
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
Get IIS site name from for an ASP.NET website
提问by S?ren Spelling Lund
In my ASP.NET web app I'd like to look up the name it was given when it was created in IIS, which is unique to the server. I'd not interested in the domain name for the web site but the actual name given to the site in IIS.
在我的 ASP.NET Web 应用程序中,我想查找它在 IIS 中创建时给出的名称,这是服务器独有的。我对网站的域名不感兴趣,但对 IIS 中为该网站提供的实际名称不感兴趣。
I need to be able to do it reliably for IIS6 and 7.
我需要能够为 IIS6 和 7 可靠地做到这一点。
To be clear I'm talking about the given name in IIS, not the domain name and not the virtual directory path.
明确地说,我在谈论 IIS 中的给定名称,而不是域名,也不是虚拟目录路径。
采纳答案by Mehdi Golchin
System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
回答by Chuck Conway
Here is a related postin retrieving the site Id.
这是检索站点 ID的相关帖子。
Here some code that might work for you:
这里有一些可能对你有用的代码:
using System.DirectoryServices;
using System;
public class IISAdmin
{
public static void GetWebsiteID(string websiteName)
{
DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
foreach(DirectoryEntry de in w3svc.Children)
{
if(de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
{
Console.Write(de.Name);
}
}
}
public static void Main()
{
GetWebsiteID("Default Web Site");
}
}
}
Here's the link to the original post.
这是原始帖子的链接。
I'm not sure if it will work on IIS7, but if you install the IIS6 compatibility components for IIS7 it should work.
我不确定它是否可以在 IIS7 上运行,但是如果您为 IIS7 安装了 IIS6 兼容性组件,它应该可以运行。
回答by ntze
You are looking for ServerManager(Microsoft.Web.Administration) which provides read and write access to the IIS 7.0 configuration system.
您正在寻找ServerManager( Microsoft.Web.Administration),它提供对 IIS 7.0 配置系统的读写访问。
Iterate through Microsoft.Web.Administration.SiteCollection, get a reference to your website using the Site Object and read the value of the Name property.
遍历 Microsoft.Web.Administration.SiteCollection,使用站点对象获取对您网站的引用并读取 Name 属性的值。
// Snippet
using (ServerManager serverManager = new ServerManager()) {
var sites = serverManager.Sites;
foreach (Site site in sites) {
Console.WriteLine(site.Name); // This will return the WebSite name
}
You can also use LINQ to query the ServerManager.Sites collection (see example below)
您还可以使用 LINQ 查询 ServerManager.Sites 集合(请参见下面的示例)
// Start all stopped WebSites using the power of Linq :)
var sites = (from site in serverManager.Sites
where site.State == ObjectState.Stopped
orderby site.Name
select site);
foreach (Site site in sites) {
site.Start();
}
Note: Microsoft.Web.Administration works onlywith IIS7.
注意:Microsoft.Web.Administration仅适用于IIS7。
For IIS6 you can use both ADSI and WMI to do this, but I suggest you to go for WMI which is faster than ADSI. If using WMI, have a look at WMI Code Creator 1.0(Free / Developed by Microsoft). It will generate the code for you.
对于 IIS6,您可以同时使用 ADSI 和 WMI 来执行此操作,但我建议您使用比 ADSI 更快的 WMI。如果使用 WMI,请查看WMI Code Creator 1.0(免费/由 Microsoft 开发)。它将为您生成代码。
HTH
HTH
回答by pungggi
You will need to do the ServerManager.OpenRemote("serverName") first when connecting to a remote server.
连接到远程服务器时,您需要先执行 ServerManager.OpenRemote("serverName") 。
Basically doing something like this
基本上做这样的事情
using (ServerManager srvMgr = ServerManager.OpenRemote("serverName"))
{
}
回答by khlr
As @belugabob and @CarlosAg already mentioned I'd rather use System.Web.Hosting.HostingEnvironment.SiteName
instead of System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName()
because IApplicationHost.GetSiteName method is not intended to be called directly! (msdn)
正如@belugabob 和@CarlosAg 已经提到的,我宁愿使用System.Web.Hosting.HostingEnvironment.SiteName
而不是System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName()
因为 IApplicationHost.GetSiteName 方法不打算直接调用!( msdn)
So you're better off using HostingEnvironment.SiteName property! (msdn)
所以你最好使用 HostingEnvironment.SiteName 属性!( msdn)
I think this should be the correct answer in respect to the documentation ;)
我认为这应该是关于文档的正确答案;)
回答by Sachin Bhale
You can use below code
您可以使用以下代码
private string WebsiteName()
{
string websiteName = string.Empty;
string AppPath = string.Empty;
AppPath = Context.Request.ServerVariables["INSTANCE_META_PATH"];
AppPath = AppPath.Replace("/LM/", "IIS://localhost/");
DirectoryEntry root = new DirectoryEntry(AppPath);
websiteName = (string)root.Properties["ServerComment"].Value;
return websiteName;
}