Asp.net background path Retrieval Methods

Source: Internet
Author: User
ArticleDirectory
    • Client Element
    • Server Control

1.Request. currentexecutionfilepathObtain the virtual path of the current request,Different fromFilepathThe difference is that if the request is already on the serverCodeIs redirected Currentexecutionfilepath Is correct

Format:/Virtual directory name/Default. aspx/Filepath/Default. aspx

2.Request. applicationpathRetrieve ServerASP. NETApplicationProgramThe root path of the virtual application. No matter where the application requests the path.

Format:/Virtual directory name(Application name)/Filepath

The above are the file paths in the virtual directory, that isIIS. To obtain a real physical path, you must add the following to the above path:
3. server. mappath (request. applicationpath + "/file/xmlfile. xml ")Map virtual paths to physical paths

Format:E :\Filepath \ file \Xmlfile. xml

4. Request. filepath or request. PathObtain the virtual path of the current request 

Format:/Filepath/default. aspx

5. Request. physicalpath;Obtain and requestURLPhysical File System Path

Format:E: \ filepath \ default. aspx

6. Request. physicalapplicationpath;Physical path of the application

Format:E: \ filepath \

7. Request. url. absoluteuriAllURL

Format:Http: // localhost: 19413/filepath/default. aspx

8. request. urlreferrer. absoluteuri you can obtain the URL related information Description: If default. aspx page Jump to default1.aspx , in default1.aspx In the background code of the page, write the above Code, we can see that it is from default. aspx jump.

Format:Http: // localhost: 19413/filepath/default. aspx

9. Request. url. Host

Format: Local isLocalhostAfter the release isWww.test.comSimilar form

10. Request. url. localpath or request. url. absolutepath

Format:/Filepath/default. aspx

11. Request. rawurlGet URL with Variable

Format:/Filepath/default2.aspx? A = Hello

Request. urlreferrer. absoluteuriRetrieve URL without Variables

Format:Http: // localhost: 19413/filepath/default. aspx

12.WhenURLYou can use:
Httpcontext. Current. Request. url. pathandquery

Format:/Filepath/default2.aspx? A = Hello

 

 

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

    • 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. This path will be parsed Based on the website root directory rather than the application root directory. 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 website is very useful.

      The path in this example assumes that the images folder is located under the root directory of the website.


      If your website 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 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. 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 ASP. NET master page overview.

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 (~). 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 demonstrates how to useImageThe server control is used 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. You cannot set~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.

 

 

 

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.