ASP.NET Web Pages - Folders
This chapter introduces the knowledge about folders and folder paths.
In this chapter, you will learn:
Logical folder structure and physical folder structure
Virtual names and physical names
Web URL and Web paths
Logical Folder Structure
Below is a typical ASP.NET website folder structure:
"Account" folder contains login and security files
"App_Data" folder contains databases and data files
"Images" folder contains images
"Scripts" folder contains browser scripts
"Shared" folder contains common files (such as layout and style files)
Physical Folder Structure
In the above website, the "Images" folder on the computer might have the following physical folder structure:
C:\Documents\MyWebSites\Demo\Images
Virtual Names and Physical Names
Taking the example above:
The virtual name for a website image might be "Images/pic31.jpg".
The corresponding physical name is "C:\Documents\MyWebSites\Demo\Images\pic31.jpg".
URL and Paths
A URL is used to access files within a website: http://www.tutorialpro.org/html/html-tutorial.html
The URL corresponds to the physical file on the server: C:\MyWebSites\tutorialpro\html\html-tutorial.html
A virtual path is a shorthand representation of a physical path. If you use a virtual path, you don't need to update the paths when you change your domain name or move your website to another server.
| URL | http://www.tutorialpro.org/html/html-tutorial.html | | Server Name | tutorialpro | | Virtual Path | /html/html-tutorial.html | | Physical Path | C:\MyWebSites\tutorialpro\html\html-tutorial.html |
The root directory of a disk drive is written as C:, but the root directory of a website is / (forward slash).
The virtual path of a web folder is usually different from its physical folder.
In your code, decide whether to use physical paths or virtual paths based on your coding needs.
ASP.NET folder paths have three tools: the ~ operator, the Server.MapPath method, and the Href method.
~ Operator
Use the ~ operator to specify a virtual path in programming code.
If you use the ~ operator, you don't need to change any of your code when your site is moved to a different folder or location:
Server.MapPath Method
The Server.MapPath method converts a virtual path (/index.html) into a physical path that the server can understand (C:\Documents\MyWebSites\Demo\default.html).
You can use this method when you need to open data files on the server (only complete physical paths can access data files):
In the next chapter of this tutorial, you will learn more about reading (and writing) data files on the server.
Href Method
The Href method converts the paths used in the code into paths that the browser can understand (the browser cannot understand the ~ operator).
You can use the Href method to create paths for resources (such as image files and CSS files).
This method is typically used in HTML elements like <a>, <img>, and <link>:
The Href method is a method of the WebPage object.