更新:2007 年 11 月
获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。
命名空间:
System.Web 程序集:
System.Web(在 System.Web.dll 中)
Public ReadOnly Property ApplicationPath As String
Dim instance As HttpRequest
Dim value As String
value = instance.ApplicationPath
public string ApplicationPath { get; }
public:
property String^ ApplicationPath {
String^ get ();
}
/** @property */
public String get_ApplicationPath()
public function get ApplicationPath () : String
可从不在根目录中的页或 Web 用户控件中使用此属性构造一个相对于应用程序根目录的 URL。这样,位于其他目录结构级别的页和共享控件可以使用相同的代码链接到应用程序中位于固定位置的资源。
下面的示例使用 Write 方法进行 HTML 编码,然后,将 ApplicationPath 属性的值写入一个文本文件。此代码示例摘自一个为 HttpRequest 类提供的更大的示例。它假设存在名为 sw 的 StreamWriter 对象。
' 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="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
Label3.Text = Request.AppRelativeCurrentExecutionFilePath
If (VirtualPathUtility.GetDirectory( _
Request.AppRelativeCurrentExecutionFilePath).Equals( _
"~/Members/")) _
Then
Image2.ImageUrl = Request.ApplicationPath & _
"/memberimages/Image1.gif"
Else
Image2.ImageUrl = Request.ApplicationPath & _
"/guestimages/Image1.gif"
End If
Label4.Text = Image2.ImageUrl
End Sub
</script>
<html >
<head runat="server">
<title>HttpRequest.ApplicationPath Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
This is the ApplicationPath from the current page:<br />
<asp:Label ID="Label1" runat="server" ForeColor="Brown" /><br />
Use it to link to resources at fixed locations in the application.<br />
<asp:Image ID="Image1" runat="server" />
<asp:Label ID="Label2" runat="server" ForeColor="Brown" />
<br /><br />
This is the AppRelativeCurrentExecutionFilePath to the current page:<br />
<asp:Label ID="Label3" runat="server" ForeColor="Brown" /><br />
Use it to help programatically construct links to resources based on the location of the current page.<br />
<asp:Image ID="Image2" runat="server" />
<asp:Label ID="Label4" runat="server" ForeColor="Brown" />
</div>
</form>
</body>
</html>
<%@ 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;
Label3.Text = Request.AppRelativeCurrentExecutionFilePath;
if (VirtualPathUtility.GetDirectory(
Request.AppRelativeCurrentExecutionFilePath).Equals("~/Members/"))
{
Image2.ImageUrl = Request.ApplicationPath +
"/memberimages/Image1.gif";
}
else
{
Image2.ImageUrl = Request.ApplicationPath +
"/guestimages/Image1.gif";
}
Label4.Text = Image2.ImageUrl;
}
</script>
<html >
<head runat="server">
<title>HttpRequest.ApplicationPath Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
This is the ApplicationPath from the current page:<br />
<asp:Label ID="Label1" runat="server" ForeColor="Brown" /><br />
Use it to link to resources at fixed locations in the application.<br />
<asp:Image ID="Image1" runat="server" />
<asp:Label ID="Label2" runat="server" ForeColor="Brown" />
<br /><br />
This is the AppRelativeCurrentExecutionFilePath to the current page:<br />
<asp:Label ID="Label3" runat="server" ForeColor="Brown" /><br />
Use it to help programatically construct links to resources based on the location of the current page.<br />
<asp:Image ID="Image2" runat="server" />
<asp:Label ID="Label4" runat="server" ForeColor="Brown" />
</div>
</form>
</body>
</html>
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0、2.0、1.1、1.0
参考