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.
When working with Master page, paths to resources are resolved based on the path of the content page.
Specifying resource paths for client elements
Elements that are not Web 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 (which is also known as absolute) URL path or various types of relative paths.
For example, if your page contains an
img element, you can set its src attribute using one of the following paths.
- An absolute URL path. An absolute URL path is useful if you are referencing resources in another location, such as an external Web site.
<img src="http://www.myexample.com/MyApplication/Images/SampleImage.jpg" />
- A site-root relative path, which is resolved against the site root (not the application root). Site-root relative paths are useful if you keep cross-application resources, such as images or client script files, in a folder that is located under the Web site root.
<img src="/MyApplication/Images/SampleImage.jpg" />
-
A relative path that is resolved against the current page path
<img src="Images/SampleImage.jpg" />
- A relative path that is resolved as a peer of the current page path
<img src="../Images/SampleImage.jpg" />
This will resolve the path to Images folder in the parent folder of current page.
Note: By default, browsers resolve relative paths by 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.
Specifying resource paths for server controls
For ASP.NET server controls, in addition to the absolute and relative paths as those for client elements, you can use Web application root operator (
~).
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.
For example, the ~ operator is used to specify a root-relative path for an image when using the Image server control.
<asp:image runat="server" id="Image1" ImageUrl="~/Images/SampleImage.jpg" />
Note: In server controls on master pages, ASP.NET dynamically modifies the URLs of properties that reference external resources.
For example, you might put an Image control on a master page and set its ImageUrl property to be relative to the master page.
At run time, ASP.NET will modify the URL so that it resolves correctly in the context of the content page.