HttpRequest.ApplicationPath Propriedade

Definição

Obtém o caminho raiz virtual da aplicação ASP.NET no servidor.

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

Valor de Propriedade

O caminho virtual da aplicação atual.

Exemplos

O exemplo seguinte utiliza o Write método para codificar em HTML e depois escrever o valor da ApplicationPath propriedade num ficheiro de texto. Este exemplo de código faz parte de um exemplo maior fornecido para a HttpRequest classe. Assume a existência de um StreamWriter objeto chamado 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))

O exemplo seguinte utiliza a ApplicationPath propriedade para construir programaticamente um caminho para um recurso que está numa localização fixa na aplicação. A página que faz referência ao recurso não tem de estar localizada no mesmo diretório que o recurso.

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

Se executar este exemplo numa aplicação Web chamada WebSite1, /WebSite1 será exibido como o valor da ApplicationPath propriedade e /WebSite1/images/Image1.gif será apresentado como o caminho completo da imagem.

Observações

Use esta propriedade para construir uma URL relativa à raiz da aplicação a partir de uma página ou controlo de utilizador Web que não esteja no diretório raiz. Isto permite que páginas e controlos partilhados que existem em diferentes níveis de uma estrutura de diretórios usem o mesmo código para ligar recursos em locais fixos na aplicação.

Aplica-se a