Home  >  Article  >  Backend Development  >  Share ASP.NET study notes (4) folder

Share ASP.NET study notes (4) folder

零下一度
零下一度Original
2017-05-24 20:47:191678browse

ASP.NET Web Pages - Folders

This chapter introduces knowledge about folders and folder paths.

In this chapter, you will learn:

Logical and Physical Folder Structure

Virtual and Physical Names

Web URL and Web path

Logical folder structure

The following is a typical ASP.NET website folder structure:

Share ASP.NET study notes (4) folder

"Account" folder Contains login and security files

"App_Data" folder contains database and data files

"Images" folder contains images

"Scripts" folder contains browser scripts

The "Shared" folder contains common files (such as layout and style files)

Physical folder structure

The "Images" folder in the above website is on your computer The physical folder structure may be as follows:

C:\Documents\MyWebSites\Demo\Images

Virtual name and physical name

Take the above example as an example :

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

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

URL and path

URL is used to access files in the website: www.w3cschool.cc/html/html-tutorial.html

URL corresponds to the Physical file: C:\MyWebSites\w3cschool\html\html-tutorial.html

Virtual path is an abbreviation of the physical path. If you use virtual paths, you do not need to update the paths when you change domain names or move your web pages to other servers.

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

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

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

ASP.NET folder paths have 3 tools: ~ operator, Server.MapPath method and Href method.

~ Operator

Use the ~ operator to specify virtual paths in programming code.

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 (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 if you provide the 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 your server.

Href method

The Href method converts the path used in the code into a path that the browser can understand (the browser cannot understand the ~ operator).

You can use the Href method to create paths to resources (such as image files and CSS files).

一般会在 HTML 中的 Share ASP.NET study notes (4) folder 元素中使用此方法:

@{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" />

Href 方法是 WebPage 对象的一种方法。

【相关推荐】

1. 分享ASP.NET学习笔记(1)--WebPages Razor

2. 分享ASP.NET学习笔记(2)--WebPages 介绍

3. 分享ASP.NET学习笔记(3)WebPages 布局

The above is the detailed content of Share ASP.NET study notes (4) folder. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn