JavaScript Editor jscript editor     Web designer 

Main Page

When working with resources in a Web site, you must often specify a path for the resource. For example, you might use a URL path to reference an image file in a page or the URL of a page elsewhere in the Web site. Similarly, code in your Web application might use a physical file path to a server-based file to read or write the file. ASP.NET provides facilities for referring to resources and for determining the paths of pages or other resources in the application.

Specifying Paths for Resources

In many cases, elements or controls on your page must refer to an external resource such as a file. ASP.NET allows you to reference external resources in various ways. The way you choose depends on whether you are working with a client-side element or a server control.

Client Elements

Elements that are not server controls on a page—client elements—are passed through as-is to the browser. Therefore, when referring to a resource from a client element, you construct paths according to standard rules for URLs in HTML. You can use a fully qualified (absolute) URL path or various types of relative paths. For example, if your page contains an img tag, you can set its src attribute using one of the following paths:

  • An absolute URL path:

    В CopyCode imageCopy Code
    <img src="" />

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

  • A site-root relative path, which is resolved against the site (not application) root. This example path assumes the existence of an Images folder under the Web site root:

    В CopyCode imageCopy Code
    <img src="/Images/SampleImage.jpg" />

    If your Web site is, the path would resolve to the following:

    В CopyCode imageCopy Code

    Site-root relative paths are useful if you keep cross-application resources such as images or client script files in a folder underneath the Web site root.

  • A relative path that is resolved against the current page path:

    В CopyCode imageCopy Code
    <img src="Images/SampleImage.jpg" />
  • A relative path that is resolved as a peer of the current page path.

    В CopyCode imageCopy Code
    <img src="../Images/SampleImage.jpg" />

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

Server Controls

In ASP.NET server controls that reference resources, you can use absolute or relative paths as you do for client elements. If you use relative paths, they are resolved relative to the path of the page, user control, or theme in which the control is contained. For example, imagine that you have a user control in a Controls folder. The user control contains an Image Web server control whose ImageUrl property is set to the following path:


When the user control runs, the path will resolve to the following:


This is true even no matter where the page is that hosts the user control.

Absolute and relative path references in a server control have the following disadvantages:

  • Absolute paths are not portable between applications. If you move the application that the absolute path points to, the links will break.

  • Relative paths in the style of client elements can be difficult to maintain if you move resources or pages to different folders.

To overcome these disadvantages, ASP.NET makes available the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root. The following examples show the ~ operator used to specify root-relative paths for an image when using the Image server control:

В CopyCode imageCopy Code
<asp:image runat="server" id="Image1"
  ImageUrl="~/Images/SampleImage.jpg" />

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


The ~ operator is recognized only for server controls and in server code. You cannot use the ~ operator for client elements.

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


In master pages, paths to resources are resolved based on the path of the content page. For more information, see ASP.NET Master Pages Overview.

Determining Physical File Paths for the Current Web Site

In your application, you might need to determine the path of a file or other resource on the server. For example, if your application reads or writes a text file programmatically, you must supply the file's complete physical path to the methods used for reading and writing.

It is not a good practice to hard-code physical file paths (such as C:\Website\MyApplication) into your application, because the paths can change if you move or deploy your application. However, ASP.NET provides you with ways to get any physical file path within your application programmatically. You can then use the base file path to create a full path to the resource you need. The two most commonly used ASP.NET features for determining a file path are properties of the HttpRequest object that return path information, and the MapPath method.


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

Determining the Path from Request Properties

The following table lists properties of the HttpRequest object that help you determine the paths of resources in your application.

The examples assume that a browser request was made using the following URL:

В CopyCode imageCopy Code

For these examples, the term "virtual path" refers to the portion of the request URL that follows the server identifier; in this case, the virtual path is the following:

В CopyCode imageCopy Code

In addition, the examples assume that the physical path for the root of the Web site is the following:

В CopyCode imageCopy Code

Finally, the examples assume the physical path contains a folder named MyPages.

Property Description


Gets the root path of the current application, regardless of where in the application you request it. For the example, the property returns the following:



Gets the virtual path of the current request. Differs from FilePath in that CurrentExecutionFilePath is correct if the request has been redirected in server code. For the example, the property returns the following:


If you get the property in 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 the example, the property returns the following:


Unlike CurrentExecutionFilePath, FilePath does not reflect server-side transfers.


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



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



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


Using the MapPath Method

The MapPath method returns the complete physical path for a virtual path that you pass to the method. For example, the following code returns the file path for the root of your Web site:

Visual BasicВ CopyCode imageCopy Code
Dim rootPath As String = Server.MapPath("~")
C#В CopyCode imageCopy Code
String rootPath = Server.MapPath("~");

The path passed to the MapPath method must be an application-relative path rather than an absolute path.

See Also

JavaScript Editor jscript editor     Web designer 
Bitcoin Gambling - The Original Crypto Dice Game