The server.mappath method is organized for reference by friends who need it./Current directory
/Website home directory
../upper directory
~/Website virtual directory
If the current website directory is E:/wwwroot, the application virtual directory is E:/wwwroot/company, and the browsed page path is E:/wwwroot/company/news/show.asp
Used in show.asp page
Server.MapPath(./) returns the path: E:/wwwroot/company/news
Server.MapPath(/) returns the path: E:/wwwroot
Server.MapPath(../) returns the path: E:/wwwroot/company
Server.MapPath(~/) returns the path: E:/wwwroot/company
server.MapPath(request.ServerVariables(Path_Info))
Request.ServerVariables(Path_Translated)
The return path of the above two methods is D:/wwwroot/company/news/show.asp
The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
grammar
Server.MapPath(Path)
parameter
Path
Specify a relative or virtual path to the physical directory to be mapped. If Path begins with a forward slash (/) or backslash (/), the MapPath method treats Path as a complete virtual path when returning the path. If Path does not begin with a slash, the MapPath method returns a path relative to an existing path in the .asp file.
Comment
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 exists on the server.
Because the MapPath method only maps paths regardless of whether the specified directory exists, you can first use the MapPath method to map the path to the physical directory structure and then pass it to the component that creates the specified directory or file on the server.
Example
For the following example, the file data.txt and the test.asp file containing the following script are located in the directory C:/Inetpub/Wwwroot/Script. The C:/Inetpub/Wwwroot directory is set as the server's home directory.
The following example uses the server variable PATH_INFO to map the physical path to 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 examples do not start with a slash character, they are relatively mapped to the current directory, in this case 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/script/data.txt<BR>
The next two examples use the slash character to specify that the returned path should be treated as the full virtual 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 forward slash (/) or back slash (/) to return the physical path of the host directory. script
<%= server.mappath(/)%><BR>
<%= server.mappath(/)%><BR>
output
c:/inetpub/wwwroot<BR>
c:/inetpub/wwwroot<BR>