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

时间:2012-06-28 18:39:08

标签: c# .net iis

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

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

哪个输出:

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

&#34; AppName的&#34;是我想要的价值,我想知道是否有另一种方法只需返回它而不必进行字符串魔术来移除路径的其余部分。

谢谢!

3 个答案:

答案 0 :(得分:6)

重申一下,根据评论主题 - ApplicationHost.GetSiteName()不打算在代码中使用(参见msdn.microsoft.com):

  

IApplicationHost.GetSiteName方法()

     

此API支持产品基础架构,但并非如此   直接从您的代码中使用。

改为使用

System.Web.Hosting.HostingEnvironment.SiteName;

Documentation at MSDN

答案 1 :(得分:0)

您可以使用此例程获取完全限定的应用程序路径,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;
        }
    }

答案 2 :(得分:0)

其中一个应该完成工作,具体取决于您的IIS应用程序/站点结构

HostingEnvironment.ApplicationHost.GetSiteName()

HostingEnvironment.ApplicationVirtualPath

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