5

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

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

哪个输出:

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

“AppName”是我想要的值,我想知道是否有另一种方法可以简单地返回它而无需执行字符串魔术来删除路径的其余部分。

谢谢!

4

3 回答 3

6

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

IApplicationHost.GetSiteName 方法 ()

此 API 支持产品基础架构,不打算直接从您的代码中使用。

而是使用

System.Web.Hosting.HostingEnvironment.SiteName;

MSDN 上的文档

于 2016-12-13T09:36:46.003 回答
1

您可以使用此例程获取完全限定的应用程序路径,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;
        }
    }
于 2012-06-28T18:45:02.147 回答
0

其中之一应该根据您的 IIS 应用程序/站点结构来完成工作

HostingEnvironment.ApplicationHost.GetSiteName()

HostingEnvironment.ApplicationVirtualPath

new ServerManager().Sites;  //This is a List so you can iterate and match with a eliminating hint
于 2015-05-18T03:26:48.253 回答