In this post, you will learn about ASP.NET web pages folders such as logical and physical folder structures, virtual and physical names, web URLs and Paths
Logical Folder Structure
Below is a typical folder structure for an ASP.NET web pages web site:
- Account folder: The "Account" folder contains logon and security files
- App_Data folder: The "App_Data" folder contains databases and data files
- Images folder: The "Images" folder contains images
- Script folder: The "Scripts" folder contains browser scripts
- Shared folder: The "Shared" folder contains common files (style sheet files and layout)
Let us assume that you create a demo website with the name Example1, the physical folder structure for the "Images" folder at the website might look like this on your computer device:
C:\Emmason\Documents\MyWebSites\Example1\Images
Looking to the physical folder above, if you have image inside the folder named "logo.gif', then the virtual name of the image might be "Images/logo.gif".
While
The physical name is
C:\Emmason\Documents\MyWebSites\Example1\Images\logo.gif
URL is an acronym for Uniform Resource Locator and URLs are used to access files from the web:
https://www.emmason247.com.ng/tutorial/Understanding-ASP-NET Web-Pages-Folders
The URL corresponds to a physical file on a server: C:\inetpub\site\tutorial.aspx
A virtual path is a way to represent physical paths of any file.
When virtual path is used, your site pages can easily be moved to a different domain (or server) without having to update the paths.
URL
https://www.emmason247.com.ng/blog/topics/what_is_url.aspx
Server name emmason247
Virtual path /blog/topics/what_is_url.aspx
Physical path C:\inetput\site\topics\what_is_url.aspx
The root on a disk drive is written like C:\, but the root on a web site is / (forward slash).
The virtual path of a web folder is (almost) never the same as the physical folder.
In your code you will, reference both the physical path and the virtual path, depending on what you are coding.
In order to work with folder paths, ASP.NET provide 3 tools for working with folder paths which are
To specify the virtual root when writing program's code, use the ~ operator.
If you use the ~ operator, instead of a path, you can move your website to a different folder or location without changing any code:
Example is
imgLogo.ImageUrl = "~/profile_pix/085.jpeg"
When the Server.MapPath is used during coding, the virtual path of the file (/pages/page2.aspx) will be converted to a physical path that the server can understand (C:\inetpub\sites\pages\page2.aspx).
Example;
imgLogo.ImageUrl = Server.MapPath("~/profile_pix/085.jpeg")
The Href method converts a path used in the code to a path that the browser can understand (the browser cannot understand the ~ operator).
You use the Href method to create paths to resources like image files, and CSS files.
You will often use this method in HTML
, , and elements:
@{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.
This content was written with idea gotten from w3schools.com