ASP.NET編程擷取網站根目錄方法小結_實用技巧

來源:互聯網
上載者:User

本文執行個體講述了ASP.NET編程擷取網站根目錄方法。分享給大家供大家參考,具體如下:

擷取網站根目錄的方法有幾種如:

Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server.MapPath("")//當前代碼檔案所在的目錄路勁
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..") 
Page.Request.ApplicationPath

以上的代碼在http://localhost/EnglishClub/manage/WebForm1.aspx頁面

運行結果:

C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
C:\Inetpub\wwwroot\
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\
C:\Inetpub\wwwroot\EnglishClub

以上的方法可以在.aspx中訪問,但是如果你在。cs檔案就不能用。

HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath

在.cs檔案中可以用。但是HttpContext.Current.Server.MapPath();這個擷取的是檔案的路徑而不是根目錄。

只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath 這個才是擷取的根目錄,在寫擷取資料庫路徑是應該用這個,其他的都有問題。

System.Web.HttpContext.Current.Request.PhysicalApplicationPath
和Server.MapPath("~/")效果是一樣的。

Server.MapPath("~/");//無論代碼所在的檔案的、頁面路勁是什麼,永遠返回 C:\Inetpub\wwwroot\EnglishClub\(就是當前程式啟動並執行所在根目錄)

如果儲存 附件的路勁 進資料庫的話,不應該把絕對路勁存進去。應該只儲存 檔案名稱部分。例如:

/uploads/abc.txt
當需要瀏覽檔案的時候,在在讀取出來的路徑:(即/uploads/abc.txt),前面+網站的路勁:例如:

http://abc.com+"/uploads/abc.txt"

補充:

ASP.NET中擷取網站根目錄和實體路徑完整執行個體:

/// <summary>/// 取得網站的根目錄的URL/// </summary>/// <returns></returns>public static string GetRootURI(){  string AppPath = "";  HttpContext HttpCurrent = HttpContext.Current;  HttpRequest Req;  if (HttpCurrent != null)  {    Req = HttpCurrent.Request;    string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);    if (Req.ApplicationPath == null || Req.ApplicationPath == "/")      //直接安裝在  Web  網站        AppPath = UrlAuthority;    else      //安裝在虛擬子目錄下        AppPath = UrlAuthority + Req.ApplicationPath;  }  return AppPath;}/// <summary>/// 取得網站的根目錄的URL/// </summary>/// <param name="Req"></param>/// <returns></returns>public static string GetRootURI(HttpRequest Req){  string AppPath = "";  if(Req != null)  {    string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);    if (Req.ApplicationPath == null || Req.ApplicationPath == "/")      //直接安裝在  Web  網站        AppPath = UrlAuthority;    else      //安裝在虛擬子目錄下        AppPath = UrlAuthority + Req.ApplicationPath;  }  return AppPath;}/// <summary>/// 取得網站根目錄的實體路徑/// </summary>/// <returns></returns>public static string GetRootPath(){  string AppPath = "";  HttpContext HttpCurrent = HttpContext.Current;  if (HttpCurrent != null)  {    AppPath = HttpCurrent.Server.MapPath("~");  }  else  {    AppPath = AppDomain.CurrentDomain.BaseDirectory;    if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)      AppPath = AppPath.Substring(0, AppPath.Length - 1);  }  return AppPath;}

希望本文所述對大家asp.net程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.