ASP. NET Website path

Source: Internet
Author: User
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, the code in the Web application can use the physical file path of the server-based file to input the file... syntaxHighlighter. all (); when using resources in 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, code in Web applications can read and write files using the physical file path of server-based files. 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 allows you to reference external resources in various ways. The selected method depends on whether the client element or server control is used.
Client Element
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.
The relative path of the site root directory, which is parsed according to 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 you keep cross-application resources (such as client script files) 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 HTMLbase 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. The user control contains an Image Web Server Control. The ImageUrl attribute of the server control is set to the following path:
Images/SampleImage.jpg
When the user control is running, the path is resolved to the following form:
/Controls/Images/SampleImage.jpg
This applies 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 disadvantages, 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 shows how to use the Image server control to specify the relative path of the root directory of the Image ~ OPERATOR:
ImageUrl = "~ /Images/SampleImage.jpg "/>
In this example, the image file is directly read from the Images folder under the Web application root directory, 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. For more information, see ASP. NET master page overview.
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 or writes a text file programmatically, the complete physical path of the file must be provided for the Read and Write methods.
Hard coding of the physical file path (such as C:/Website/MyApplication) to the application is not a good practice, because if you move or deploy the application, the path will change. 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 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.
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.
 
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 FilePath, the difference is that if the request has been redirected in the server code, CurrentExecutionFilePath is correct. 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 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
Copy code
Dim rootPath As String = Server. MapPath ("~ ")
C #
Copy code
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.

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.