Бөлісу құралы:


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

Значение свойства

Виртуальный путь текущего приложения.

Примеры

В следующем примере метод используется Write для html-кодирования, а затем записывает значение ApplicationPath свойства в текстовый файл. Этот пример кода является частью более крупного примера, предоставленного для HttpRequest класса. Предполагается существование StreamWriter объекта с именем 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, /WebSite1 будет отображаться в качестве значения ApplicationPath свойства и /WebSite1/images/Image1.gif будет отображаться как полный путь к изображению.

Комментарии

Используйте это свойство, чтобы создать URL-адрес относительно корневого каталога приложения из страницы или веб-элемента управления пользователя, который не находится в корневом каталоге. Это позволяет страницам и общим элементам управления, существующим на разных уровнях структуры каталогов, использовать один и тот же код для связывания с ресурсами в фиксированных расположениях в приложении.

Применяется к