HttpRequest.ApplicationPath HttpRequest.ApplicationPath HttpRequest.ApplicationPath HttpRequest.ApplicationPath Property

定义

获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。Gets the ASP.NET application's virtual application root path on the server.

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

属性值

当前应用程序的虚拟路径。The virtual path of the current application.

示例

下面的示例使用Write方法对进行 HTML 编码,然后将ApplicationPath属性的值写入文本文件。The following example uses the Write method to HTML-encode and then write the value of the ApplicationPath property to a text file. 此代码示例是为HttpRequest类提供的更大示例的一部分。This code example is part of a larger example provided for the HttpRequest class. 它假定存在一个StreamWriter名为sw的对象。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属性以编程方式构造指向应用程序中固定位置的资源的路径。The following example uses the ApplicationPath property to programmatically construct a path to a resource that is in a fixed location in the application. 引用资源的页面不必与资源位于同一个目录中。The page that references the resource does not have to be located in the same directory as the resource.

<%@ 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 的 Web 应用程序中运行此示例, /WebSite1则将显示为ApplicationPath属性的值,并/WebSite1/images/Image1.gif将显示为图像的完整路径。If you run this example in a Web application that is named WebSite1, /WebSite1 will be displayed as the value of the ApplicationPath property and /WebSite1/images/Image1.gif will be displayed as the complete path of the image.

注解

使用此属性,可以从根目录之外的页面或 Web 用户控件构造相对于应用程序根的 URL。Use this property to construct a URL relative to the application root from a page or Web user control that is not in the root directory. 这允许在目录结构的不同级别上存在的页和共享控件使用相同的代码链接到应用程序中固定位置的资源。This allows pages and shared controls that exist at different levels of a directory structure to use the same code to link to resources at fixed locations in the application.

适用于