ASP. NET Web Pages-folders
This chapter provides information about folder and folder paths.
In this chapter, you will learn:
Logical folder structure and physical folder structure
Virtual name and physical name
Web URL and Web path
Logical folder structure
The following is a typical ASP. NET site folder structure:
The account folder contains login and security files
The "App_Data" folder contains databases and data files
The "Images" folder contains pictures
The "Scripts" folder contains browser scripts
The "Shared" folder contains common files (such as layouts and style files)
Physical folder structure
The physical folder structure on the computer in the "Images" folder on the above Web site may be as follows:
C:\Documents\MyWebSites\Demo\Images
Virtual name and physical name
Take the example above:
The virtual name of the site picture may be "images/pic31.jpg".
The corresponding physical name is "C:\Documents\MyWebSites\Demo\Images\pic31.jpg".
URLs and Paths
URLs are used to access files in a Web site: www.w3cschool.cc/html/html-tutorial.html
The URL corresponds to the physical file on the server: C:\MyWebSites\w3cschool\html\html-tutorial.html
A virtual path is a shorthand representation of a physical path. If you use a virtual path, you can not update the path when you change the domain name or move your page to a different server.
The root directory of the disk drive is written as C:, but the root directory of the Web site is/(slash).
The virtual path of a Web folder is typically not the same as a physical folder.
In your code, you decide to use the physical path and the virtual path, depending on your coding needs.
The ASP. Net Folder path has 3 tools: the ~ operator, the Server.MapPath method, and the Href method.
~ operator
Use the ~ operator to specify the virtual path in the programming code.
If you use the ~ operator to migrate your site to a different folder or location, you don't have to change any of your code:
var myimagesfolder = "~/images"; var mystylesheet = "~/styles/stylesheet.css";
Server.MapPath method
The Server.MapPath method transforms the virtual path (/index.html) into a physical path (C:\Documents\MyWebSites\Demo\default.html) that the server can understand.
When you need to open the data file on the server, you can use this method (only provide the full physical path to access the data file):
var pathName = "~/datafile.txt"; var fileName = Server.MapPath (pathName);
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 transforms the path used in the code into a path that the browser can understand (the browser does not understand the ~ operator).
You can use the Href method to create paths to resources (like files and CSS files).
This method is typically used in the <a>, , 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 way to webpage an object.
"Recommended"
1. Share the ASP. NET Learning Notes (1)--webpages Razor
2. share the ASP. NET Learning Notes (2)--webpages Introduction
3. share the ASP. NET Learning Notes (3) Webpages layout