获取网站根目录Url

 /// <summary>
        /// 获取网站根目录Url
        /// </summary>
        public static string Root
        {
            get
            {
                if (System.Web.HttpContext.Current.Cache["WebAppRoot"] == null)
                {
                    string AppPath = string.Empty;
                    HttpContext httpCurrent = HttpContext.Current;
                    HttpRequest request;
                    request = httpCurrent.Request;
                    string UrlAuthority = request.Url.GetLeftPart(UriPartial.Authority);
                    if (request.ApplicationPath == null || request.ApplicationPath == "/")//直接安装在Web站点 
                    {
                        AppPath = UrlAuthority;
                    }
                    else //安装在虚拟子目录下
                    {
                        AppPath = UrlAuthority + request.ApplicationPath;
                    }

                    //return AppPath;

                    System.Web.HttpContext.Current.Cache.Insert("WebAppRoot", AppPath, null, DateTime.Now.AddHours(1), TimeSpan.Zero);

                }
                return (string)System.Web.HttpContext.Current.Cache["WebAppRoot"];
            }
        }

 

posted @ 2013-11-15 13:57  Lukas89  阅读(562)  评论(0编辑  收藏  举报