获得网站根目录

2015-12-05 09:24:17  访问(1330) 赞(0) 踩(0)

  /// <summary>
        /// 获得网站根目录(比如网站虚拟目录tools) - http://www.slowx.net/tools/
        /// </summary>
        /// <returns></returns>
        public static string GetWebRootUrl()
        {
            HttpContext hc = HttpContext.Current;

            if (hc == null)
            {
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:HttpContext hc = HttpContext.Current值为null。"
                    );
            }

            // 虚拟目录加完整参数页面地址 //
            // /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
            string strPathAndQuery = hc.Request.Url.PathAndQuery;

            // 完整URL地址 //
            string strAbsoluteUri = hc.Request.Url.AbsoluteUri;

            int idx = strAbsoluteUri.LastIndexOf(strPathAndQuery);

            if (idx == -1)
            {

                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "int idx = strAbsoluteUri[" + strAbsoluteUri + "].LastIndexOf(strPathAndQuery[" + strPathAndQuery + "]) == -1;"
                    );
            }

            string theResult = strAbsoluteUri.Substring(0, idx);

            if (hc.Request.ApplicationPath == "/")
                return theResult;
            else
                return theResult + hc.Request.ApplicationPath;
        }


标签:获得网站根目录 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)