Nash
Nash

Reputation: 952

Get IIS Application Name in .NET App

I am currently using the following code:

<center>Application Name: <%=HostingEnvironment.ApplicationID %></center>

Which outputs:

Application Name: /LM/W3SVC/1/Root/AppName

"AppName" is the value I want and I'm wondering whether there is another method that will simply return it without having to do string magic to remove the rest of the path.

Thanks!

Upvotes: 5

Views: 7921

Answers (3)

JOAT
JOAT

Reputation: 188

One of these should do the job depending on what your IIS app/site structure is

HostingEnvironment.ApplicationHost.GetSiteName()

HostingEnvironment.ApplicationVirtualPath

new ServerManager().Sites;  //This is a List so you can iterate and match with a eliminating hint

Upvotes: 0

EvilDr
EvilDr

Reputation: 9632

To reiterate, based on the comment thread - ApplicationHost.GetSiteName() is not intended to be used in code (see msdn.microsoft.com):

IApplicationHost.GetSiteName Method ()

This API supports the product infrastructure and is not intended to be used directly from your code.

Instead use

System.Web.Hosting.HostingEnvironment.SiteName;

Documentation at MSDN

Upvotes: 6

DotNetUser
DotNetUser

Reputation: 6612

you can use this routine to get fully qualified application path, context.Request.ApplicationPath will contain application name

    /// <summary>
    /// Return full path of the IIS application
    /// </summary>
    public string FullyQualifiedApplicationPath
    {
        get
        {
            //Getting the current context of HTTP request
            var context = HttpContext.Current;

            //Checking the current context content
            if (context == null) return null;

            //Formatting the fully qualified website url/name
            var appPath = string.Format("{0}://{1}{2}{3}",
                                        context.Request.Url.Scheme,
                                        context.Request.Url.Host,
                                        context.Request.Url.Port == 80
                                            ? string.Empty
                                            : ":" + context.Request.Url.Port,
                                        context.Request.ApplicationPath);

            if (!appPath.EndsWith("/"))
                appPath += "/";

            return appPath;
        }
    }

Upvotes: 2

Related Questions