How to use Server.MapPath in asp
I have always forgotten how to use Server.MapPath. Let’s record it below for later use:
General Note: The paths obtained by Server.MapPath are all physical paths on the server, which is often called absolute paths.
1. Server.MapPath("/")
Note: Get the location where the application root directory is located, such as C:/Inetpub/wwwroot/.
2. Server.MapPath("./")
Note: Get the current directory of the page, which is equivalent to Server.MapPath("").
3. Server.MapPath("../")
Note: Get the upper directory of the page you are on.
4. Server.MapPath("~/")
Note: Obtain the directory of the current application-level program. If it is the root directory, it is the root directory. If it is a virtual directory, it is the location of the virtual directory, such as C:/Inetpub/wwwroot/Example/.
Let's look at the picture first:
1. First, create the red folder under the E disk of this machine, and continue to create the conn.asp file in the folder. The file content is as follows:
<%=Server.Mappath("database/cnbruce.mdb")%> <%=Server.Mappath("/database/cnbruce.mdb")%> <%=Server.Mappath("../database/cnbruce.mdb")%> |
The results of the final display of the three paths are debugged.
2. Create a folder database under the red folder, and the cnbruce.mdb file is stored inside the folder.
So now we can know for sure that the absolute path of the database file is: E:/red/database/cnbruce.mdb
There should be no problem with the above. If you understand it, continue reading it. If you don’t understand, learn the Windows operating system-_-!
3. For comparison and comparison, continue to create a blue folder under the red folder, and the same conn.asp file is stored inside the folder.
<%=Server.Mappath("database/cnbruce.mdb")%> <%=Server.Mappath("/database/cnbruce.mdb")%> <%=Server.Mappath("../database/cnbruce.mdb")%> |
4. Of course, it is very important to set the virtual directory of the red folder. The method is to right-click the red folder, select "Share...", continue to select the "Web Sharing" tab in the pop-up window, select "Share this folder", and set the alias to "red". Finally "OK".
5. OK. Now you can debug two conn.asp files.
First look at the red folder. The URL entered should be: http://localhost/red/conn.asp
You can see that only one of them is correct, that is
<%=Server.Mappath("database/cnbruce.mdb")%>
6. Let’s look at the blue folder in the red folder. URL: http://localhost/red/blue/conn.asp