2010-08-11 94 views
0

我正在使用它來獲取應用程序根目錄,並想知道這是否是一種更新的更好方法?如何獲取Web應用程序根目錄?

string root = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/'); 

回答

3

它看起來可能會過度,但這是我使用的幫手例程。我還沒有遇到任何麻煩。

public class UrlHelper 
{ 
    public static string ApplicationBaseUrl() 
    { 
     string port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; 

     if (port == null || port == "80" || port == "443") 
      port = ""; 
     else 
      port = ":" + port; 

     string protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"]; 

     if (protocol == null || protocol == "0") 
      protocol = "http://"; 
     else 
      protocol = "https://"; 

     return protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + 
       HttpContext.Current.Request.ApplicationPath; 
    } 
} 
+0

謝謝,我會試試看。 – VoodooChild 2010-08-11 01:06:57

相關問題