X



Home>Tutorial>

Understanding ASP.NET Web Pages Folders,Server.MapPath and Href Operator

Nuelson Penuel Sunday, September 13, 2020 ASP.Net

Summary

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:

Web Pages Folders

  • 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)

Physical Folder Structure

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
 

Virtual and Physical Names

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

URLs and Paths

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

Virtual Path

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

The ~ Operator

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"

The Server.MapPath Method

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

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

| | Views(609)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services