HttpServerUtility.MapPath(String) 方法

定义

返回对应于指定虚拟路径的物理文件路径。

public:
 System::String ^ MapPath(System::String ^ path);
public string MapPath(string path);
member this.MapPath : string -> string
Public Function MapPath (path As String) As String

参数

path
String

Web 应用程序中的虚拟路径。

返回

与 .. 对应的 pathWeb 服务器上的物理文件路径。

例外

当前 HttpContextnull.

-或-

path 是物理路径,但预期是虚拟路径。

示例

以下示例演示如何检索相对虚拟路径的物理文件。 代码驻留在网页的代码隐藏文件中,并利用默认 Server 对象。

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string pathToFiles = Server.MapPath("/UploadedFiles");
    }
}
Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim pathToFiles = Server.MapPath("/UploadedFiles")
    End Sub
End Class

下一个示例类似于前面的示例,只不过它演示如何从不在代码隐藏文件中的类中检索物理路径。

public class SampleClass
{
    public string GetFilePath()
    {
        return HttpContext.Current.Server.MapPath("/UploadedFiles");
    }
}
Public Class SampleClass
    Public Function GetFilePath() As String
        Return HttpContext.Current.Server.MapPath("/UploadedFiles")
    End Function
End Class

注解

null如果是path,该方法MapPath将返回包含路径当前请求的目录的完整物理路径。 相对路径不需要为此方法指定现有文件或文件夹即可返回值。 但是,不能指定 Web 应用程序外部的路径。

Important

该方法 MapPath 可能包含有关托管环境的敏感信息。 不应向用户显示返回值。

驻留在何处 C:\ExampleSites\TestMapPath 的 Web 应用程序将返回以下结果:

请求发件人 path 返回的值
RootLevelPage.aspx null C:\ExampleSites\TestMapPath
RootLevelPage.aspx “/DownOneLevel/DownLevelPage.aspx” C:\ExampleSites\TestMapPath\DownOneLevel\DownLevelPage.aspx
RootLevelPage.aspx “/NotRealFolder” C:\ExampleSites\TestMapPath\NotRealFolder
RootLevelPage.aspx "../OutsideApplication” HttpException
/DownOneLevel/DownLevelPage.aspx null C:\ExampleSites\TestMapPath\DownOneLevel
/DownOneLevel/DownLevelPage.aspx "../RootLevelPage.aspx” C:\ExampleSites\TestMapPath\RootLevelPage.aspx

适用于