ASP MapPath 方法


Server 對象參考手冊 完整的 Server 對象參考手冊

MapPath 方法可把指定的路徑映射到伺服器上相應的物理路徑上。

注意:此方法不能用於 Session.OnEnd 和 Application.OnEnd 中。

語法

Server.MapPath(path)

參數 描述
path 必需。映射為物理路徑的相對路徑或絕對路徑。如果該參數以 / 或 \ 開頭,則返回完整的虛擬路徑。如果該參數不以 / 或 \ 開頭,則返回相對於正在被處理的 .asp 檔的路徑。

實例

實例 1

舉例,檔 test.asp 位於 C:\Inetpub\wwwroot\Script。

檔 Test.asp (位於 C:\Inetpub\wwwroot\Script) 包含下列代碼:

<%
response.write(Server.MapPath("test.html") & "<br>")
response.write(Server.MapPath("script/test.html") & "<br>")
response.write(Server.MapPath("/script/test.html") & "<br>")
response.write(Server.MapPath("script") & "<br>")
response.write(Server.MapPath("/") & "<br>")
response.write(Server.MapPath("") & "<br>")
%>

輸出:

c:inetpubwwwrootscripttest.html
c:inetpubwwwrootscriptscripttest.html
c:inetpubwwwrootscripttest.html
c:inetpubwwwrootscript
c:inetpubwwwroot
c:inetpubwwwroot

實例 2

如何使用一個相對路徑來返回到達正在流覽器中查看的頁面的相對物理路徑:

<%
response.write(Server.MapPath("../"))
%>

或者:

<%
response.write(Server.MapPath(".."))
%>


Server 對象參考手冊 完整的 Server 對象參考手冊