HttpRequest.ApplicationPath 属性

定义

获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。

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

属性值

String

当前应用程序的虚拟路径。

示例

以下示例使用 Write 该方法对 HTML 编码,然后将属性的值 ApplicationPath 写入文本文件。 此代码示例是为类提供的大型示例的 HttpRequest 一部分。 It assumes the existence of a StreamWriter object named sw.

// Write request information to the file with HTML encoding.
sw.WriteLine(Server.HtmlEncode(DateTime.Now.ToString()));
sw.WriteLine(Server.HtmlEncode(Request.CurrentExecutionFilePath));
sw.WriteLine(Server.HtmlEncode(Request.ApplicationPath));
sw.WriteLine(Server.HtmlEncode(Request.FilePath));
sw.WriteLine(Server.HtmlEncode(Request.Path));
' Write request information to the file with HTML encoding.
sw.WriteLine(Server.HtmlEncode(DateTime.Now.ToString()))
sw.WriteLine(Server.HtmlEncode(Request.CurrentExecutionFilePath))
sw.WriteLine(Server.HtmlEncode(Request.ApplicationPath))
sw.WriteLine(Server.HtmlEncode(Request.FilePath))
sw.WriteLine(Server.HtmlEncode(Request.Path))

以下示例使用 ApplicationPath 该属性以编程方式构造位于应用程序中固定位置的资源的路径。 引用资源的页不必与资源位于同一目录中。

<%@ 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;
    }
</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>
        ApplicationPath:<br />
        <asp:Label ID="Label1" runat="server" ForeColor="Brown" /><br />

        <asp:Image ID="Image1" runat="server" /><br />

        ImageUrl:<br />
        <asp:Label ID="Label2" runat="server" ForeColor="Brown" />
        <br />
        </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
    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>
        ApplicationPath:<br />
        <asp:Label ID="Label1" runat="server" ForeColor="Brown" /><br />

        <asp:Image ID="Image1" runat="server" />

        ImageUrl:<br />
        <asp:Label ID="Label2" runat="server" ForeColor="Brown" />
        <br />
     </div>
    </form>
</body>
</html>

如果在名为 WebSite1 的 ApplicationPath Web 应用程序中运行此示例,/WebSite1则会显示为属性的值,并/WebSite1/images/Image1.gif显示为图像的完整路径。

注解

使用此属性从不在根目录中的页面或 Web 用户控件构造相对于应用程序根目录的 URL。 这允许位于目录结构不同级别的页面和共享控件使用相同的代码链接到应用程序中固定位置的资源。

适用于