Related paths used in Asp.net Development

Source: Internet
Author: User

Paths can be divided into absolute paths and relative paths. In Asp.net, the relative path expressions of client controls and server controls are quite different.

The details are as follows:

 

Client Element

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

 
2. relative root directory
 (If your website is a http://www.contoso.com, this path resolves to the form below.
http://www.contoso.com/Images/SampleImage.jpg

)

3. relative to the current directory
Server Control
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. On the master page, the resource path is parsed Based on the path on the Content 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.

 

<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.

Note:

Only for mobile pages. If your application depends on a non-Cookie Session or you may receive a request from a mobile device that requires a non-Cookie Session, use the waveform character ("~") in the path. A new session may be created accidentally and session data may be lost. To use a waveform character (for example, "~ /Path) to set the property of the mobile control. Use the resolveurl method to parse the path and then assign it to the property.

 

Determine the physical file path of the current website

 

1. determine the path based on the request property

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

 

2. Use the mappath Method

The mappath method returns the complete physical path of the virtual path passed to the method.

String rootPath = Server.MapPath("~");
String rootPath = HostingEnvironment.MapPath("~/image/aa/bb");
For more details, see msdn.

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.