asp.net
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. ASP.net 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. ASP.net 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 http://www.contoso.com, the path resolves to the following form:
Http://www.contoso.comhttp://www.pushad.com/http://www.pushad.com/Info/Images/SampleImage.jpg
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.
Attention |
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 ASP.net 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:
http://www.pushad.com/Info/Images/SampleImage.jpg
When the user control runs, the path resolves to the following form:
/controlshttp://www.pushad.com/http://www.pushad.com/info/images/sampleimage.jpg
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, asp.net enables the Web application root operator (~), which can be used when specifying a path in a server control. asp.net 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= "~http://www.pushad.com/http://www.pushad.com/info/images/" 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.
Attention |
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.
Attention |
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 asp.net 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, ASP.net 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 asp.net features used to determine file paths are the properties of HttpRequest objects that return path information, and the MapPath method.
Attention |
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:
Http://www.contoso.com/MyApplication/MyPages/Default.aspx
For these examples, the term "virtual path" is part of the request URL following the server identifier, where the virtual path looks like this:
/myapplication/mypages/default.aspx
In addition, these examples assume that the physical path to the Web site root is the following form:
C:\inetpub\wwwroot\MyApplication\
Finally, these examples assume that the physical path contains a folder named Mypages.
Property |
Description |
Applicationpath |
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: / |
Currentexecutionfilepath |
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: /myapplication/mypages/default.aspx 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. |
FilePath |
Gets the virtual path of the current request. For this example, the property returns the following: /myapplication/mypages/default.aspx Unlike Currentexecutionfilepath,FilePath does not reflect server-side conversions. |
Path |
Gets the virtual path of the current request. For this example, the property returns the following: /myapplication/mypages/default.aspx |
Physicalapplicationpath |
Gets the physical file system path of the root directory of the currently executing application. For this example, the property returns the following: C:\inetpub\wwwroot\ |
PhysicalPath |
Gets the physical file system path that corresponds to the requested URL. For this example, the property returns the following: C:\inetpub\wwwroot\MyApplication\MyPages\default.aspx |
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 ("~");
Attention |
The path passed to the MapPath method must be an application-relative path, not an absolute path. |