HttpRequest.ApplicationPath Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient le chemin racine de l’application virtuelle de l’application ASP.NET sur le serveur.
public:
property System::String ^ ApplicationPath { System::String ^ get(); };
public string ApplicationPath { get; }
member this.ApplicationPath : string
Public ReadOnly Property ApplicationPath As String
Valeur de propriété
Chemin d’accès virtuel de l’application actuelle.
Exemples
L’exemple suivant utilise la Write méthode pour encoder html, puis écrire la valeur de la ApplicationPath propriété dans un fichier texte. Cet exemple de code fait partie d’un exemple plus grand fourni pour la HttpRequest classe. Elle suppose l’existence d’un StreamWriter objet nommé 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))
L’exemple suivant utilise la ApplicationPath propriété pour construire par programmation un chemin d’accès à une ressource située à un emplacement fixe dans l’application. La page qui référence la ressource n’a pas besoin d’être située dans le même répertoire que la ressource.
<%@ 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>
Si vous exécutez cet exemple dans une application web nommée WebSite1, /WebSite1 s’affiche comme valeur de la ApplicationPath propriété et /WebSite1/images/Image1.gif s’affiche comme chemin d’accès complet de l’image.
Remarques
Utilisez cette propriété pour construire une URL par rapport à la racine de l’application à partir d’une page ou d’un contrôle utilisateur web qui ne se trouve pas dans le répertoire racine. Cela permet aux pages et aux contrôles partagés qui existent à différents niveaux d’une structure d’annuaire d’utiliser le même code pour établir un lien vers des ressources à des emplacements fixes dans l’application.