Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET Web Pages Folders

ASP.NET Web Pages - Folders


This chapter introduces knowledge about folders and folder paths.


In this chapter, you will learn:


Logical Folder Structure

Below is a typical ASP.NET website folder structure:


Physical Folder Structure

The physical folder structure of the "Images" folder in the website above on the computer might be as follows:

C:\Documents\MyWebSites\Demo\Images


Virtual Names and Physical Names

Taking the example above:

The virtual name of a website image might be "Images/pic31.jpg".

The corresponding physical name is "C:\Documents\MyWebSites\Demo\Images\pic31.jpg".


URL and Path

A URL is used to access files on a website: http://www.runoob.com/html/html-tutorial.html

The URL corresponds to the physical file on the server: C:\MyWebSites\runoob\html\html-tutorial.html

A virtual path is a shorthand representation of a physical path. If you use a virtual path, you do not need to update the path when you change the domain name or move your web pages to another server.

URL http://www.runoob.com/html/html-tutorial.html
Server Name RUNOOB
Virtual Path /html/html-tutorial.html
Physical Path C:\MyWebSites\runoob\html\html-tutorial.html

The root of a disk drive is written as C:, but the root of a website is / (slash).

A web folder's virtual path 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 3 tools: the ~ operator, the Server.MapPath method, and the Href method.


The ~ Operator

Use the ~ operator to specify a virtual path in programming code.

If you use the ~ operator, you do not need to change any of your code when your site is moved to a different folder or location:

var myImagesFolder = "~/images";

var myStyleSheet = "~/styles/StyleSheet.css";


The Server.MapPath Method

The Server.MapPath method converts a virtual path (/index.html) into a physical path (C:\Documents\MyWebSites\Demo\default.html) that the server can understand.

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 full physical path):

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.


The Href Method

The Href method converts the path used in code into a path 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).

It is generally used in the HTML 、 and element with this method:

@{var myStyleSheet = "~/Shared/Site.css";}

The Href method is a method of the WebPage object.