This chapter provides information about folders and folder paths.
In ASP.NET WebPages, there are two folder structures, namely, logical folder structure and physical folder structure.
In this chapter you will learn:
Logical folder structure and physical folder structure
Virtual and physical names
Web URLs and Web paths
The following is a typical ASP.NET website folder structure:
The "Account" folder contains login and security files
"App_Data" folder contains database and data files
The "Images" folder contains images
The "Scripts" folder contains browser scripts
The "Shared" folder contains common files (such as layout and style files)
The physical folder structure of the "Images" folder in the above website on your computer might be as follows:
C:DocumentsMyWebSitesDemoImages
Take the example above:
The virtual name for a website image might be "Images/pic31.jpg".
The corresponding physical name is "C:DocumentsMyWebSitesDemoImagespic31.jpg".
The URL is used to access files on the website: //www.w3cschool.cn/html/html-tutorial.html
The URL corresponds to the physical file on the server: C:MyWebSitesw3cschoolhtmlhtml-tutorial.html
A virtual path is a shorthand representation of a physical path. If you use virtual paths, you don't need to update the paths when you change domain names or move your web pages to other servers.
URL | //www.w3cschool.cn/html/html-tutorial.html |
Server name | w3cschool |
virtual path | /html/html-tutorial.html |
physical path | C:MyWebSitesw3cschoolhtmlhtml-tutorial.html |
The root directory of a disk drive is written as C:, but the root directory of a website is / (slash).
The virtual path of a Web folder is usually different from the physical folder.
In your code, decide between physical and virtual paths based on your coding needs.
There are 3 tools for ASP.NET folder paths: ~ operator, Server.MapPath method and Href method.
Use the ~ operator to specify virtual paths in programming code.
If you use the ~ operator, you can move your site to a different folder or location without changing any of your code:
var myImagesFolder = "~/images"; var myStyleSheet = "~/styles/StyleSheet.css";
The Server.MapPath method converts the virtual path (/index.html) into a physical path that the server understands (C:DocumentsMyWebSitesDemodefault.html).
When you need to open a data file on the server, you can use this method (the data file can only be accessed by providing the complete physical path):
var pathName = "~/dataFile.txt"; var fileName = Server.MapPath(pathName);
In the next chapter of this tutorial, you'll learn more about reading (and writing) data files on the server.
The Href method converts the path used in the code into a path that the browser can understand (the browser cannot understand the ~ operator).
You can use the Href method to create paths to resources such as image files and CSS files.
This method is typically used within the <a>, <img> and <link> elements in HTML:
@{var myStyleSheet = "~/Shared/Site.css";} <!-- This creates a link to the CSS file. --> <link rel="stylesheet" type="text/css" href="@ Href(myStyleSheet)" /> <!-- Same as : --> <link rel="stylesheet" type="text/css" href="/Shared/Site.css" />
The Href method is a method of the WebPage object.