努力學習

為了將來~

取得 Web 應用程式的根目錄及URL目錄的方法

發表留言

學習記錄

如果是要取得Web 應用程式的實體目錄:

  • Server.MapPath("/")
    應用程序根目錄所在的位置  如 C:\Inetpub\wwwroot\
  • Server.MapPath("./")
    表示所在頁面的目錄
    注:等於Server.MapPath("") 返回 Server.MapPath("") 所在頁面的物理文件路徑
  • Server.MapPath("../")
    表示上一層目錄
  • Server.MapPath("~/")
    表示當前應用程序的目錄,如果是根目錄,就是根目錄,如果是虚擬目錄,就是虚擬目錄所在的位置如 C:\Inetpub\wwwroot\Example\

如果是要取得Web 應用程式的URL目錄,可以使用Request.Url.AbsoluteUri來取得

Dim idx As Integer = Request.Url.AbsoluteUri.IndexOf(Request.ApplicationPath)
Dim serviceUri As String = Request.Url.AbsoluteUri.Substring(0, idx) 
'伺服器 URL   
Dim applicationUri As String = serviceUri + Request.ApplicationPath 
'應用程式 Context URL

 

參考資料:

如何取得 Web 應用程式的根目錄 Context URL

ASP.NET中Server.MapPath() 和 Request.MapPath()使用區别

發表留言