Phương pháp ASP MapPath


❮ Tham chiếu đối tượng máy chủ hoàn chỉnh

Phương thức MapPath ánh xạ một đường dẫn cụ thể đến một đường dẫn vật lý.

Lưu ý: Không thể sử dụng phương pháp này trong Session.OnEnd và Application.OnEnd.

Cú pháp

Server.MapPath(path)

Parameter Description
path Required. A relative or virtual path to map to a physical path. If this parameter starts with / or \, it returns a path as if this parameter is a full virtual path. If this parameter doesn't start with / or \, it returns a path relative to the directory of the .asp file being processed

Các ví dụ

ví dụ 1

Ví dụ bên dưới, tệp "test.asp" nằm trong C: \ Inetpub \ Wwwroot \ Script.

Tệp "test.asp" (nằm trong C: \ Inetpub \ Wwwroot \ Script) chứa mã sau:

<%
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>")
%>

Output:

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

Ví dụ 2

Cách sử dụng đường dẫn tương đối để trả lại đường dẫn vật lý tương đối đến trang đang được xem trong trình duyệt:

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

or

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

❮ Tham chiếu đối tượng máy chủ hoàn chỉnh