[Reference] ASP. NET Website path

Source: Internet
Author: User

A. Specify the path of the client element Resource
B. Specify the path of the server control resource
C. Determine the physical file path of the current website
======================================

When using resources on a website, you must specify the resource path.
For example, you can use a URL path to reference the URL of an image file on a page or a page at another location on a website.
Similarly, Web ApplicationsProgramInCodeYou can use the physical file path of the server-based file to read and write the file.
ASP. NET provides a method for referencing resources and determining the paths of pages or other resources in an application.

A. Specify the path of the client element Resource
------------------------------
In many cases, elements or controls on a page must reference external resources, such as files.
ASP. NET allows you to reference external resources in various ways.
The selected method depends on whether the client element or server control is used.

Elements (not server controls in the page, but client elements) are transmitted to the browser as they are.
Therefore, when referencing resources from the client element, the path should be constructed according to the standard URL rules in HTML.
You can use a fully qualified (absolute) URL path or various types of relative paths.
For example, if the page contains an IMG tag, you can use one of the following paths to set its src attribute:

Absolute URL path:

If you reference resources in other locations (such as external websites), the absolute URL path is very useful.

Relative Path of the site root directory,
It is parsed Based on the root directory of the site (not an application.
The path in this example assumes that the images folder exists in the root directory of the website:

If your website is a http://www.contoso.com, the path resolves to the form below:
Http://www.contoso.com/Images/SampleImage.jpg
If cross-application resources (such as client script files)
If it is stored in the folder under the root directory of the website, the relative path of the root directory of the site is very useful.

The relative path parsed based on the current page path:

Resolved to the relative path of the current page path peer to peer.

Note:
By default, the browser uses the URL of the current page as the reference to parse the relative path.
However, you can include the HTML base element in the page to specify an alternative base path.
 

B. specify the server control resource path
---------------------------
in the ASP. in the. NET Server Control,
the absolute or relative path can be used, which is the same as the client element.
If a relative path is used, the path relative to the page, user control, or topic containing the control is parsed.
For example, assume that the controls folder contains a user control.
this user control contains an image Web Server Control.
the imageurl attribute of this server control is set to the following path:
images/sampleimage.jpg
when the user control is running, the path is parsed as follows:
/controls/images/sampleimage.jpg
This is true regardless of the page location where the user control is hosted.

Absolute and relative path references in server controls have the following Disadvantages:
Absolute paths cannot be transplanted between applications.
If you move the absolute path to the application, the link will be interrupted.
If you move resources or pages to different folders,
It may be difficult to maintain the relative path using the client element style.
To overcome these shortcomings,
ASP. NET enables the web application root directory operator (~),
You can use this operator when specifying a path in the server control.
ASP. NET will ~ The operator is resolved to the root directory of the current application.
Can be used in combination ~ Operator and folder to specify the path based on the current root directory.
The following example
Demonstrate how to use the image server control to specify the relative path of the root directory of the image ~ OPERATOR:
<Asp: Image runat = "server" id = "image1"
Imageurl = "~ /Images/sampleimage.jpg "/>
In this example,
The image file will be directly read from the images folder under the root directory of the Web application,
No matter where the page is located.
Note:
~ The operator can only be recognized by the server control and is located in the server code.
Cannot change ~ The operator is used for client elements.

It can be used in any path-related attribute in the server control ~ Operator.
Note:
On the master page, the resource path is parsed Based on the path on the Content Page.
 

C. Determine the physical file path of the current website
----------------------------
In an application, you may need to determine the path of files or other resources on the server.
For example, if an application reads and writes text files programmatically,
The complete physical path of the file must be provided for the Read and Write methods.

Place the physical file path (for example, c: \ website \ myapplication)
Hard coding is not a good practice in applications,
Because if you move or deploy an application, the path is changed.
However,
ASP. NET provides you with a way to obtain any physical file path in an application programmatically.
Then, you can use the base file path to create the complete path of the required resources.
The two most common ASP. NET functions used to determine the file path are:
Return the attributes of the httprequest object in the path information and the mappath method.
Note:
Do not send the physical file path to the client,
Because they may be used by malicious users to obtain information about your applications.

Determine the path based on the request property
The following table lists the attributes of the httprequest object,
These attributes help you determine the path of resources in the application.
The following example assumes that the following URL is used to send browser requests:
Http://www.contoso.com/MyApplication/MyPages/Default.aspx
For these examples,
The term "virtual path" refers to a part of the request URL following the server identifier,
The virtual path is as follows:
/Myapplication/mypages/default. aspx
In addition, these examples assume that the physical path of the website root directory is in the following format:
C: \ Inetpub \ wwwroot \ myapplication \
Finally, these examples assume that the physical path contains a folder named mypages.
The relevant attributes and returned values are as follows:
Applicationpath
Obtain the root directory path of the current application,
The path is requested no matter where the application is located.
In this example, the property returns the following content:
/

Currentexecutionfilepath
Obtain the virtual path of the current request.
Different from filepath,
The difference is that if the request has been redirected in the server code,
Then, currentexecutionfilepath is correct.
In this example, the property returns the following content:
/Myapplication/mypages/default. aspx
If it is the result of calling transfer or execute,
If you obtain the properties of the running code, the path will reflect the location of the Code.

Filepath
Obtain the virtual path of the current request.
In this example, the property returns the following content:
/Myapplication/mypages/default. aspx
Unlike currentexecutionfilepath, filepath does not reflect server-side conversion.

Path
Obtain the virtual path of the current request. In this example, the property returns the following content:
/Myapplication/mypages/default. aspx

Physicalapplicationpath
Obtain the physical file system path of the root directory of the currently running application.
In this example, the property returns the following content:
C: \ Inetpub \ wwwroot \

Physicalpath
Obtain the physical file system path corresponding to the requested URL.
In this example, the property returns the following content:
C: \ Inetpub \ wwwroot \ myapplication \ mypages \ default. aspx

Use the mappath Method
The mappath method returns the complete physical path of the virtual path passed to the method.
For example, the following code returns the file path of the root directory of the website:
Visual Basic:
Dim rootpath as string = server. mappath ("~ ")
C #:
String rootpath = server. mappath ("~ ");
Note:
The path passed to the mappath method must be the relative path of the application, rather than the absolute path.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.