./Current Directory
/Website home directory
../Upper directory
~ /Website virtual directory
If the current website directory is E: \ wwwroot, the web page path browsed by E: \ wwwroot \ company is E: \ wwwroot \ company \ news \ show. asp
Use
Server. MapPath ("./") Return Path: E: \ wwwroot \ company \ news
Server. MapPath ("/") Return Path: E: \ wwwroot
Server. MapPath ("../") Return Path: E: \ wwwroot \ company
Server. MapPath ("~ /") Return Path: E: \ wwwroot \ company
Server. MapPath (request. ServerVariables ("Path_Info "))
Request. ServerVariables ("Path_Translated ")
The preceding two methods return the path D: \ wwwroot \ company \ news \ show. asp.
The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
Syntax
Server. MapPath (Path)
Parameters
Path
Specifies the relative or virtual path of the physical directory to be mapped. If the Path starts with a forward slash (/) or backslash (\), The MapPath method regards the Path as a complete virtual Path when returning the Path. If the Path does not start with a slash, The MapPath method returns the Path relative to the existing Path in the. asp file.
Note
The MapPath method does not support relative path syntax (.) or (..). For example, the following relative path ../MyDir/MyFile.txt returns an error.
The MapPath method does not check whether the returned path is correct or whether it exists on the server.
Because the MapPath method only maps paths regardless of whether the specified directory exists, you can first map the paths of the physical directory structure with the MapPath method, it is then passed to the component that creates the specified directory or file on the server.
Example
For the following example, the data.txt file and the test. asp file containing the following scripts are all in the C: \ Inetpub \ Wwwroot \ Script directory. The C: \ Inetpub \ Wwwroot directory is set as the host directory of the server.
The following example uses the server variable PATH_INFO to map the physical path of the current file. Script
<% = Server. mappath (Request. ServerVariables ("PATH_INFO") %> <BR>
Output
C: \ inetpub \ wwwroot \ script \ test. asp <BR>
Because the path parameters in the following example do not start with a slash, they are mapped to the current directory, where C: \ Inetpub \ Wwwroot \ Script. Script
<% = Server. mappath ("data.txt") %> <BR>
<% = Server. mappath ("script/data.txt") %> <BR>
Output
C: \ inetpub \ wwwroot \ script \ data.txt <BR>
C: \ inetpub \ wwwroot \ script \ data.txt <BR>
The following two examples use the slash character to specify the full virtual path of the returned path on the server. Script
<% = Server. mappath ("/script/data.txt") %> <BR>
<% = Server. mappath ("\ script") %> <BR>
Output
C: \ inetpub \ script \ data.txt <BR>
C: \ inetpub \ script <BR>
The following example shows how to use a forward slash (/) or backslash (\) to return the physical path of the home directory. Script
<% = Server. mappath ("/") %> <BR>
<% = Server. mappath ("\") %> <BR>
Output
C: \ inetpub \ wwwroot <BR>
C: \ inetpub \ wwwroot <BR>