ASP MapPath 方法


❮ 完整的 Server 服务器对象参考

MapPath 方法可把指定的路径影射到服务器上相应的物理路径上。

注释: 此方法不能用于 Session.OnEnd 和 Application.OnEnd 中。

语法

Server.MapPath(path)

参数 描述
path 必需。映射为物理路径的相对路径或绝对路径。如果该参数以 / 或 \ 开头,则返回完整的虚拟路径。如果该参数不以 / 或 \ 开头,则返回相对于正在被处理的 .asp 文件的路径。

实例

实例 1

For the example below, the file "test.asp" is located in C:\Inetpub\Wwwroot\Script.

The file "test.asp" (located in C:\Inetpub\Wwwroot\Script) contains the following code:

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

输出:

c:\inetpub\wwwroot\script\test.asp
c:\inetpub\wwwroot\script\script\test.asp
c:\inetpub\wwwroot\script\test.asp
c:\inetpub\wwwroot\script
c:\inetpub\wwwroot
c:\inetpub\wwwroot

实例 2

How to use a relative path to return the relative physical path to the page that is being viewed in the browser:

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

or

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

❮ 完整的 Server 服务器对象参考