ASP. NET web project path
. NET framework 4
Other Versions
When using resources in a web project, 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, the code in the web project 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.
Path of the specified resource
In many cases, elements or controls on a page must reference external resources, such as files. ASP. NET supports various methods to reference external resources. The reference method selected varies depending on whether you are using the client element or Web Server Control.
Client Element
Client elements are non-Web Server Control Elements on the page, which are passed 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 URL path (also known as an absolute URL path) or various types of relative paths. For example, if the page contains an IMG element, you can use one of the following paths to set its SRC feature:
Absolute URL path. If you reference resources in other locations (such as external websites), the absolute URL path is very useful.
The relative path of the website root directory, which will be parsed Based on the website root directory. If the resources (such as client script files) used by the entire site are kept in the folder under the root directory of the website, the relative path of the root directory of the website is very useful.
The following example assumes that the images folder is located in the root directory of the website.
If the site URL is a http://www.contoso.com, this path resolves to the form below:
http://www.contoso.com/Images/SampleImage.jpg
The following is a 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 benchmark for parsing relative paths. However, you can include the HTML base element in the page to specify an alternative base path. |
Server Control
In the ASP. NET Server Control that references resources, you can use an absolute or relative path, which is the same as the client element. If the relative path is used, it is parsed relative to the path of the page, user control, or topic containing the control. 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 above path will be resolved to the following form:/controls/images/sampleimage.jpg. The results are the same no matter where the page that hosts the user control is located.
Note: |
On the master page, the resource path is parsed Based on the path on the Content Page. For more information, see the ASP. NET master page. |
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 disadvantages, ASP. NET includes the web application root directory operator (~), This operator can be used when you specify 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 demonstrates how to use the image server control to specify the relative path of the root directory for the image ~ Operator. In this example, the image file is directly read from the images folder in the Web application root directory no matter where the page is located on the website.
<asp:image runat="server" id="Image1" ImageUrl="~/Images/SampleImage.jpg" />
It can be used in any path-related attribute in the server control ~ Operator. ~ 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.
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 a WEB Project performs read/write operations on a text file programmatically, the complete physical path of the file must be provided for reading and writing methods.
Hard coding of physical file paths (such as c: \ website \ myapplication) to applications is not a good practice, because these paths will be changed if you move or deploy applications. In addition, if you deploy the site to the bearer provider, you may not know the specific physical path. However, ASP. NET provides you with a way to obtain any physical file path in a web project 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 the attributes of the httprequest object that returns the path information and the mappath method.
Note: |
Physical file paths should not be sent 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 examples listed in the table are based on the following assumptions:
You have used the following URL to send a browser request: http://www.contoso.com/myapplication/mypages/default.aspx.
The term "virtual path" refers to the part of the request URL following the server identifier. In this example, the virtual path refers to the following path:/myapplication/mypages/default. aspx.
The physical path of the website root directory is: C: \ Inetpub \ wwwroot \ myapplication \.
This physical path contains a folder named mypages.
Attribute |
Description |
Applicationpath |
Obtain the root directory path of the current application. In this example, the property returns the following content :/ |
Currentexecutionfilepath |
Obtain the virtual path of the current request. Unlike the filepath attribute, currentexecutionfilepath is correct if the request has been redirected in the server code. In this example, the property returns the following content:/myapplication/mypages/default. aspx If you obtain the properties in the running code as a result of calling transfer or execute, 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 the currentexecutionfilepath attribute, filepath does not reflect transmission on the server. |
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 |
Convert a virtual path to a physical path
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: