Updated: July 2009
Gets the virtual path of the application root and makes it relative by using the tilde (~) notation for the application root (as in "~/page.aspx").
Namespace:
System.Web
Assembly:
System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public ReadOnly Property AppRelativeCurrentExecutionFilePath As String
Dim instance As HttpRequest
Dim value As String
value = instance.AppRelativeCurrentExecutionFilePath
public string AppRelativeCurrentExecutionFilePath { get; }
public:
property String^ AppRelativeCurrentExecutionFilePath {
String^ get ();
}
public function get AppRelativeCurrentExecutionFilePath () : String
Property Value
Type:
System..::.StringThe virtual path of the application root for the current request.
Use this property to provide URL information that will stay the same even if the application changes location. This allows the same URL-mapping code to be used in a test environment and in the final deployment environment, or to be used by copies of Web applications in different domains.
The following example uses the AppRelativeCurrentExecutionFilePath property to set the URL of an Image control to an image in the same directory as the page. Run this page at different levels of a directory structure to see the resulting AppRelativeCurrentExecutionFilePath property values.
<%@ Page Language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Get an image that is in the same directory as the currently executing control.
Image1.ImageUrl = VirtualPathUtility.GetDirectory( _
Request.AppRelativeCurrentExecutionFilePath) + "image1.jpg"
Label1.Text = "App-relative Image URL = " + Image1.ImageUrl
End Sub
</script>
<head id="Head1" runat="server">
<title>HttpRequest AppRelativeCurrentExecutionFilePath</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" /><br />
<asp:Label ID="Label1" runat="server" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// Get an image that is in the same directory as the currently executing control.
Image1.ImageUrl =
VirtualPathUtility.GetDirectory(Request.AppRelativeCurrentExecutionFilePath)
+ "image1.jpg";
Label1.Text = "App-relative Image URL = " + Image1.ImageUrl;
}
</script>
<head id="Head1" runat="server">
<title>HttpRequest AppRelativeCurrentExecutionFilePath</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" /><br />
<asp:Label ID="Label1" runat="server" />
</div>
</form>
</body>
</html>
The following example uses the AppRelativeCurrentExecutionFilePath property to programmatically set the path to a resource, based on the current path of the page.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = Request.ApplicationPath
Image1.ImageUrl = Request.ApplicationPath + "/images/Image1.gif"
Label2.Text = Image1.ImageUrl
Label3.Text = Request.AppRelativeCurrentExecutionFilePath
If (VirtualPathUtility.GetDirectory( _
Request.AppRelativeCurrentExecutionFilePath).Equals( _
"~/Members/")) _
Then
Image2.ImageUrl = Request.ApplicationPath & _
"/memberimages/Image1.gif"
Else
Image2.ImageUrl = Request.ApplicationPath & _
"/guestimages/Image1.gif"
End If
Label4.Text = Image2.ImageUrl
End Sub
</script>
<html >
<head runat="server">
<title>HttpRequest.ApplicationPath Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
This is the ApplicationPath from the current page:<br />
<asp:Label ID="Label1" runat="server" ForeColor="Brown" /><br />
Use it to link to resources at fixed locations in the application.<br />
<asp:Image ID="Image1" runat="server" />
<asp:Label ID="Label2" runat="server" ForeColor="Brown" />
<br /><br />
This is the AppRelativeCurrentExecutionFilePath to the current page:<br />
<asp:Label ID="Label3" runat="server" ForeColor="Brown" /><br />
Use it to help programatically construct links to resources based on the location of the current page.<br />
<asp:Image ID="Image2" runat="server" />
<asp:Label ID="Label4" runat="server" ForeColor="Brown" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.ApplicationPath;
Image1.ImageUrl = Request.ApplicationPath + "/images/Image1.gif";
Label2.Text = Image1.ImageUrl;
Label3.Text = Request.AppRelativeCurrentExecutionFilePath;
if (VirtualPathUtility.GetDirectory(
Request.AppRelativeCurrentExecutionFilePath).Equals("~/Members/"))
{
Image2.ImageUrl = Request.ApplicationPath +
"/memberimages/Image1.gif";
}
else
{
Image2.ImageUrl = Request.ApplicationPath +
"/guestimages/Image1.gif";
}
Label4.Text = Image2.ImageUrl;
}
</script>
<html >
<head runat="server">
<title>HttpRequest.ApplicationPath Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
This is the ApplicationPath from the current page:<br />
<asp:Label ID="Label1" runat="server" ForeColor="Brown" /><br />
Use it to link to resources at fixed locations in the application.<br />
<asp:Image ID="Image1" runat="server" />
<asp:Label ID="Label2" runat="server" ForeColor="Brown" />
<br /><br />
This is the AppRelativeCurrentExecutionFilePath to the current page:<br />
<asp:Label ID="Label3" runat="server" ForeColor="Brown" /><br />
Use it to help programatically construct links to resources based on the location of the current page.<br />
<asp:Image ID="Image2" runat="server" />
<asp:Label ID="Label4" runat="server" ForeColor="Brown" />
</div>
</form>
</body>
</html>
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0
Reference
Date | History | Reason |
|---|
July 2009
| Simplified code example. |
Customer feedback.
|