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

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 containsIMGYou can use one of the following paths to setSRCAttribute:

  • 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 HTMLBaseTo 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 useImageWhen the server control is used 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 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. You cannot set~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 listsHttprequestObject Attributes, which 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.

 
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. Different from filepath, the difference is that if the request has been redirected in the server codeCurrentexecutionfilepathIs 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

Different fromCurrentexecutionfilepath,FilepathServer-side conversion is not reflected.

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

MappathMethod 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:

Copy code in Visual Basic
 
DimRootpathAs 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.

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.