Get IIS Site Name for ASP.NET Website

In my ASP.NET web application, I would like to find the name that it was given when it was created in IIS, which is unique to the server. I would not be interested in the domain name for the website, but the actual name indicated on the website in IIS.

I need to be able to do this reliably for IIS6 and 7.

To be clear, I'm talking about a name in IIS, not a domain name, not a virtual directory path.

The value from IIS that I would like to read from C # http://img252.imageshack.us/img252/6621/capturedz.png

+48
c # iis
Oct 31 '09 at 16:04
source share
6 answers
System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName(); 
+55
Oct 31 '09 at 18:03
source share

The following is a message looking for a site identifier.

Here is the 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 is a link to the original post .

I'm not sure that it will work on IIS7, but if you install the IIS6 compatibility components for IIS7, it should work.

+9
Oct 31 '09 at 16:32
source share

You are looking for a ServerManager (Microsoft.Web.Administration) that provides read and write access to the IIS 7.0 configuration.

Iterating through Microsoft.Web.Administration.SiteCollection, get a link to your site using the site object and read the value of the Name property.

 // 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)

 // 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 only works with IIS7 .

For IIS6, you can use both ADSI and WMI, but I suggest you upgrade to WMI, which is faster than ADSI. If you are using WMI, check out WMI Code Creator 1.0 (free / developed by Microsoft). It will generate code for you.

NTN

+9
Nov 01 '09 at 13:51
source share

As @belugabob and @CarlosAg already mentioned, I prefer to use System.Web.Hosting.HostingEnvironment.SiteName instead of System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName() , because the IApplicationHost.GetSiteName method is not intended to be called directly! ( msdn )

So, you better use the HostingEnvironment.SiteName property! ( msdn )

I think this should be the correct answer regarding documentation;)

+9
Jun 26 '13 at 17:55
source share

First you need to connect to the remote ServerManager.OpenRemote server ("server_name").

Basically something like that

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

see msdn help

0
Apr 6 2018-11-11T00:
source share

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; } 
0
Nov 03 '14 at 17:47
source share



All Articles