Web site path

Source: Internet
Author: User
Tags copy file system reference resource root directory

When working with resources in a Web site, you typically must specify the path to the resource. For example, you can use a URL path to refer to an image file in a page or the URL of a page at another location in a Web site. Similarly, code in a WEB application can read and write files using the physical file path of a server-based file. provides methods for referencing resources and determining the path of pages or other resources in an application.

Specify the path to the resource

In many cases, an element or control on a page must reference an external resource, such as a file. allows you to refer to external resources in a variety of ways. The selected method depends on whether you are using a client element or a server control.

Client element

Element (not the server control in the page, but the client element) is passed to the browser as-is. Therefore, when referencing resources from a client element, the path should be constructed according to the Standard Rules for URLs in HTML. You can use a fully qualified (absolute) URL path, or you can use a relative path of various types. For example, if a page contains an img tag, you can set its src property by using one of the following paths:

  • Absolute URL Path:

    An absolute URL path is useful if you are referencing a resource in another location, such as an external Web site.

  • The site root relative path, which is parsed based on the site (not the application) root directory. This example path assumes that the Images folder exists in the root directory of the site:

    If your site is, the path resolves to the following form:


    The site root relative path is useful if you leave a cross application resource, such as an image or client script file, in a folder in the root directory of the Web site.

  • Relative paths resolved based on the current page path:

  • Resolves to the relative path of the current page path equivalent.


    By default, browsers resolve relative paths using the URL of the current page as the Datum. However, you can include the HTML base element in the page to specify an alternate base path.

Server control

In server controls that reference resources, you can use absolute or relative paths, as is the case with client elements. If you use a relative path, it is resolved relative to the path of the page, user control, or theme that contains the control. For example, suppose the Controls folder contains a user control. The user control contains an Image Web server control that is set to the following path for the ImageUrl property of the server control:

When the user control runs, the path resolves to the following form:


This is true regardless of the page location that hosts the user control.

The absolute and relative path references in server controls have the following disadvantages:

    • Absolute paths are not portable between applications. If you move an application that the absolute path points to, the link is interrupted.

    • If you move a resource or page to a different folder, it may be difficult to maintain a relative path that takes a client-side element style.

To overcome these disadvantages, enables the Web application root operator (~), which can be used when specifying a path in a server control. resolves the ~ operator to the root directory of the current application. You can use the ~ operator and the folder to specify the path based on the current root directory. The following example shows the ~ operator used to specify the root-relative path of an image when using the images server control:

<asp:image runat= "Server" id= "Image1" imageurl= "~" Sampleimage.jpg "/>

In this example, the image file is read directly from the Images folder in the Web application root directory, regardless of where the page is located in the Web site.


The ~ operator is recognized only for server controls and is located in server code. The ~ operator cannot be used with the client element.

You can use the ~ operator in any path-related property in a server control.


In the master page, the path of the resource is parsed based on the path of the content page. For more information, see Overview of master pages .

Determine the physical file path for the current Web site

In your application, you may need to determine the path to a file or other resource on the server. For example, if your application reads and writes text files programmatically, you must provide the full physical path of the file for the method used for reading and writing.

It is not a good idea to hard-code physical file paths (such as C:\Website\MyApplication) into an application because the path changes if you move or deploy the application. However, provides you with a way to programmatically get any physical file paths in your application. You can then use the base file path to create the full path to the resource you want. Two of the most common features used to determine file paths are the properties of HttpRequest objects that return path information, and the MapPath method.


Physical file paths should not be sent to the client because they may be used by a malicious user to get information about your application.

Determining the path based on the request properties

The following table lists the properties of the HttpRequest object, which help you determine the path to the resources in your application.

The following example assumes that the following URL is used to emit a browser request:


For these examples, the term "virtual path" is part of the request URL following the server identifier, where the virtual path looks like this:


In addition, these examples assume that the physical path to the Web site root is the following form:


Finally, these examples assume that the physical path contains a folder named Mypages.

Property Description


Gets the root path of the current application, regardless of the location in the application where the path is requested. For this example, the property returns the following:



Gets the virtual path of the current request. Unlike FilePath, the difference is that if the request has been redirected in server code, currentexecutionfilepath is correct. For this example, the property returns the following:


If you obtain a property in the code that is running as a result of a call to Transfer or Execute , the path reflects the location of the code.


Gets the virtual path of the current request. For this example, the property returns the following:


Unlike Currentexecutionfilepath,FilePath does not reflect server-side conversions.


Gets the virtual path of the current request. For this example, the property returns the following:



Gets the physical file system path of the root directory of the currently executing application. For this example, the property returns the following:



Gets the physical file system path that corresponds to the requested URL. For this example, the property returns the following:


Using the MapPath method

The MapPath method returns the full physical path of the virtual path passed to the method. For example, the following code returns the file path to the root of the Web site:

Visual Basic Copy Code
Dim RootPath as String = Server.MapPath ("~")
C# Copy Code
String RootPath = Server.MapPath ("~");

The path passed to the MapPath method must be an application-relative path, not an 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: 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.