1. 程式人生 > >asp.net 獲取網站根地址

asp.net 獲取網站根地址

 1 public static string GetSiteRoot()
 2         {
 3             string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
 4             if (port == null || port == "80" || port == "443")
 5             {
 6                 port = "";
 7             }
 8             else
 9
{ 10 port = ":" + port; 11 } 12 string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"]; 13 if (protocol == null || protocol == "0") 14 { 15 protocol = "http://"; 16 }
17 else 18 { 19 protocol = "https://"; 20 } 21 string sOut = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath; 22 if (sOut.EndsWith("
/")) 23 { 24 sOut = sOut.Substring(0, sOut.Length - 1); 25 } 26 return sOut; 27 }