2009-10-31 117 views
49

在我的ASP.NET Web应用程序中,我想查看它在IIS中创建时的名称,该名称在服务器中是唯一的。我对网站的域名不感兴趣,但是在IIS中给这个网站的实际名称。从ASP.NET网站获取IIS站点名称

我需要能够做到这一点可靠地IIS6和7

要清楚我说的是在IIS中给定的名称,而不是域名,而不是虚拟目录路径。

Value from IIS I'd like to read from C# http://img252.imageshack.us/img252/6621/capturedz.png

+0

你能否解释多一点的动机是什么? – 2009-10-31 16:20:44

+1

我正在为我编写的Web应用程序授权,我需要确保每个Web应用程序只使用一次许可证密钥,即使在同一台服务器上也是如此。该Web应用程序与分配给IIS站点的多个域名一起工作,因此将该密钥绑定到该站点的域名不会。 在同样徒劳的绑定中,机器名称的关键字不会执行任何操作,因为我的应用程序可能安装在同一台机器上的多个客户机上。 总之,我需要在特定机器上唯一标识IIS网站的内容。由于IIS不允许具有相同名称的多个站点,我认为这是要走的路。 – 2009-10-31 16:25:17

+0

我不是故意冒充居高临下,我只是好奇为什么有人会想这样做。 – 2009-10-31 16:25:41

回答

55
System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName(); 
+0

这正是我一直在寻找。非常感谢。你知道它是否适用于IIS6和7? – 2009-11-02 18:28:34

+0

对不起,我没有在IIS 6中测试它。 – 2009-11-02 18:47:54

+1

我测试了几分钟的IIS 7/5.1,我认为它适用于IIS 6(90%)。 – 2009-11-02 19:09:48

10

这里是检索网站ID related post

这里是一些代码,可能会为你工作:

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

}

这里的链接到original post

我不确定它是否可以在IIS7上工作,但是如果您安装IIS7的IIS6兼容性组件,它应该可以工作。

9

您正在寻找ServerManagerMicrosoft.Web.Administration),其提供的读写访问IIS 7.0配置系统。

迭代Microsoft.Web.Administration.SiteCollection,使用Site Object获取对您的网站的引用并读取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 
} 

您还可以使用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(); 
     } 

注意:Microsoft.Web.Administration工作IIS7

对于IIS6,您可以同时使用ADSI和WMI来做到这一点,但我建议您选择比ADSI更快的WMI。如果使用WMI,请看WMI Code Creator 1.0(由Microsoft开发/免费开发)。它会为你生成代码。

HTH

0

连接到远程服务器时,您需要做的ServerManager.OpenRemote(“服务器名称”)第一。

基本上做这样的事情

  using (ServerManager srvMgr = ServerManager.OpenRemote("serverName")) 
      { 

      } 

see msdn help

10

由于@belugabob和@CarlosAg已经提到我宁愿使用System.Web.Hosting.HostingEnvironment.SiteName代替System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName()因为IApplicationHost.GetSiteName方法不打算直接调用!(msdn

所以你最好使用HostingEnvironment.SiteName属性! (msdn

我想这应该是对于文档正确答案;)

+1

谢谢你指出这一点。在我的情况下System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName()的工作,但后来我遇到了一个weired“类型不解析成员”与序列化,我无法追查。切换到HostingEnvironment.SiteName后,它消失了。我认为它与MarshalByRefObject与序列化不兼容有关。以防万一有人受到这个打击,切换到HostingEnvironment.SiteName解决了这个问题。 – 2013-06-30 10:39:39

0

您可以使用下面的代码

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