共用方式為


HttpRequest.AppRelativeCurrentExecutionFilePath 屬性

定義

取得應用程式根目錄的虛擬路徑,並對應用程式根目錄使用波狀符號 (~) 標記法,讓此路徑變成相對路徑 (就像是 "~/page.aspx")。

public:
 property System::String ^ AppRelativeCurrentExecutionFilePath { System::String ^ get(); };
public string AppRelativeCurrentExecutionFilePath { get; }
member this.AppRelativeCurrentExecutionFilePath : string
Public ReadOnly Property AppRelativeCurrentExecutionFilePath As String

屬性值

目前要求的應用程式根目錄之虛擬路徑。

範例

下列範例會 AppRelativeCurrentExecutionFilePath 使用 屬性,將控制項的 Image URL 設定為與頁面相同目錄中的影像。 在不同的目錄結構層級執行此頁面,以查看產生的 AppRelativeCurrentExecutionFilePath 屬性值。

<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<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>
<%@ Page Language="VB"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<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>

下列範例會 AppRelativeCurrentExecutionFilePath 使用 屬性,根據頁面的目前路徑,以程式設計方式設定資源的路徑。

<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
<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="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 xmlns="http://www.w3.org/1999/xhtml" >
<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>

備註

使用這個屬性來提供 URL 資訊,即使應用程式變更位置,也會維持不變。 這允許在測試環境和最終部署環境中使用相同的 URL 對應程式碼,或供不同網域中的 Web 應用程式複本使用。

適用於