在.NET App中获取IIS应用程序名称

xeo*_*eon 5 .net c# iis

我目前正在使用以下代码:

<center>Application Name: <%=HostingEnvironment.ApplicationID %></center>
Run Code Online (Sandbox Code Playgroud)

哪个输出:

Application Name: /LM/W3SVC/1/Root/AppName
Run Code Online (Sandbox Code Playgroud)

"AppName"是我想要的值,我想知道是否有另一种方法只需返回它而不必执行字符串魔法来删除路径的其余部分.

谢谢!

Evi*_*lDr 6

重申一下,基于注释线程 - ApplicationHost.GetSiteName()不打算在代码中使用(请参阅msdn.microsoft.com):

IApplicationHost.GetSiteName方法()

此API支持产品基础结构,不能直接在您的代码中使用.

而是使用

System.Web.Hosting.HostingEnvironment.SiteName;
Run Code Online (Sandbox Code Playgroud)

MSDN上的文档


Dot*_*ser 2

您可以使用此例程获取完全限定的应用程序路径, context.Request.ApplicationPath 将包含应用程序名称

    /// <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;
        }
    }
Run Code Online (Sandbox Code Playgroud)