取得当前网站地址和当前虚拟目录的地址

取得当前网站地址和当前虚拟目录的地址:
 1        /// <summary>
 2        /// 取得当前虚拟目录地址
 3        /// 如:http://localhost:2791/ 或 http://localhost/XXXX
 4        /// </summary>
 5        /// <returns></returns>

 6        protected string GetCurrentAppPath()
 7        {
 8            string virtualPath = Request.ApplicationPath;
 9
10            StringBuilder sb = new StringBuilder();
11            sb.Append(GetCurrentDomainPath());
12
13            if(virtualPath.Substring(virtualPath.Length-1!= "/")
14                sb.Append('/');
15
16            return sb.ToString();
17        }

18        
19        /// <summary>
20        /// 取得当前网站地址
21        /// 如:http://localhost:2791 或 http://localhost
22        /// </summary>
23        /// <returns></returns>

24        protected string GetCurrentDomainPath()
25        {
26            string servername = Request.ServerVariables["SERVER_NAME"];
27            string port = Request.ServerVariables["SERVER_PORT"];
28
29            StringBuilder sb = new StringBuilder();
30            sb.Append("http://");
31            sb.Append(servername);
32            if (port.Length > 0)
33            {
34                sb.Append(":");
35                sb.Append(port);
36            }

37            return sb.ToString();
38        }
posted on 2008-04-01 10:40  Eric huang  阅读(358)  评论(0编辑  收藏  举报