Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET web Pages folder


May 12, 2021 ASP.NET


Table of contents


ASP.NET Web Pages - Folder


This chapter describes the knowledge of folders and folder paths.

In ASP.NET Web Pages, there are two folder structures: logical folder structure and physical folder structure.


In this chapter, you'll learn:

  • Logical folder structure and physical folder structure
  • The virtual and physical names
  • Web URL and web path

The logical folder structure

Here is a ASP.NET site folder structure:

ASP.NET web Pages folder

  • The Account folder contains logins 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 public files (such as layout and style files)

Physical folder structure

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

C:\Documents\MyWebSites\Demo\Images


The virtual and physical names

Take the example above:

The virtual name of the site image may be "Images/pic31 .jpg".

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


URL and path

The URL is used to access files in the Web site: /www.w3cschool.cn/html/html-tutorial.html

The URL corresponds to the physical file on the server: C: s/MyWebSites/w3cschool?html-tutorial.html

A virtual path is a short-form representing a physical path. If you use a virtual path, you don't have to update the path when you change the domain name or move your page to another server.

Url www.w3cschool.cn/html/html-tutorial.html
The name of the server w3cschool
The virtual path /html/html-tutorial.html
The physical path C:\MyWebSites\w3cschool\html\html-tutorial.html

The root of the disk drive is written as follows C: , but the root of the web site is / (slash).

The virtual path of a Web folder is usually different from that of a physical folder.

In your code, decide to use physical and virtual paths based on your encoding needs.

ASP.NET folder paths have three tools: the operator, the Server.MapPath method, and the Href method.


The operator

The virtual path is specified in the programming code using the operator.

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";


Server.MapPath method

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

You can use this method when you need to open a data file on the server (data files can only be accessed by providing a 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

The Href method converts 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 such as image files and CSS files.

This method is typically used in elements of 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 one way for WebPage objects.

Related tutorials

HTML reference manual