Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get IIS Web Site Application Name

Tags:

c#

.net

iis

I'm trying to get the web application name I'm currently in. (Where my application code is deployed in IIS).

I can get the IIS server name:

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.

like image 344
13 revs, 8 users 76% Avatar asked Jun 06 '12 17:06

13 revs, 8 users 76%


2 Answers

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.

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
like image 191
Alexander Collins Avatar answered Oct 19 '22 17:10

Alexander Collins


Add the following reference to your application: "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);
    }
}
like image 31
Hovo Avatar answered Oct 19 '22 17:10

Hovo