Get IIS site name from for an ASP.NET website

C#asp.netIis

C# Problem Overview


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.

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

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

C# Solutions


Solution 1 - C#

Update

As Carlos and others mentioned in the comments, it's better to use HostingEnvironment.SiteName since GetSiteName is not supposed to be used by users (According to the docs).

Old Solution

System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();

Solution 2 - C#

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)

So you're better off using HostingEnvironment.SiteName property! (msdn)

I think this should be the correct answer in respect to the documentation ;)

Solution 3 - C#

Here is a related post in retrieving the site 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.

Solution 4 - C#

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

Iterate through Microsoft.Web.Administration.SiteCollection, get a reference to your website 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 works only with 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.

HTH

Solution 5 - C#

You will need to do the ServerManager.OpenRemote("serverName") first when connecting to a remote server.

Basically doing something like this

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

see msdn help

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSøren Spelling LundView Question on Stackoverflow
Solution 1 - C#Mehdi GolchinView Answer on Stackoverflow
Solution 2 - C#Jan KöhlerView Answer on Stackoverflow
Solution 3 - C#Chuck ConwayView Answer on Stackoverflow
Solution 4 - C#ntzeView Answer on Stackoverflow
Solution 5 - C#pungggiView Answer on Stackoverflow
Solution 6 - C#Sachin BhaleView Answer on Stackoverflow