Latest web development tutorials

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對象參考手冊