HttpServerUtility.MapPath(String) HttpServerUtility.MapPath(String) HttpServerUtility.MapPath(String) HttpServerUtility.MapPath(String) Method


返回与指定虚拟路径相对应的物理文件路径。Returns the physical file path that corresponds to the specified virtual path.

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


String String String String

Web 应用程序中的虚拟路径。The virtual path in the Web application.


对应于 path 的 Web 服务器上的物理文件路径。The physical file path on the Web server that corresponds to path.


当前 HttpContextnullThe current HttpContext is null.

-or- path 是物理路径,但应为虚拟路径。path is a physical path, but a virtual path was expected.


下面的示例演示如何检索相对虚拟路径的物理文件。The following example shows how to retrieve the physical file of a relative virtual path. 代码驻留在代码隐藏文件中, 并使用默认Server对象。The code resides in the code-behind file for a web page and utilizes the default Server object.

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

下面的示例与前面的示例类似, 只不过它演示如何从代码隐藏文件之外的类中检索物理路径。The next example is similar to the previous example except it shows how to retrieve a physical path from within a class that is not in the code-behind file.

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


如果path MapPathnull, 则此方法返回包含路径的当前请求的目录的完整物理路径。If path is null, the MapPath method returns the full physical path of the directory that contains the current request for the path. 相对路径不需要为此方法指定一个现有的文件或文件夹来返回值。The relative path does not need to specify an existing file or folder for this method to return a value. 但是, 不能指定 Web 应用程序之外的路径。However, you cannot specify a path outside of the Web application.


MapPath方法可能包含有关宿主环境的敏感信息。The MapPath method potentially contains sensitive information about the hosting environment. 不应向用户显示返回值。The return value should not be displayed to users.

驻留在上C:\ExampleSites\TestMapPath的 Web 应用程序将返回以下结果:A Web application that resides at C:\ExampleSites\TestMapPath would return the following results:

请求来自Request from path 返回值Returned value
RootLevelPage.aspxRootLevelPage.aspx null C:\ExampleSites\TestMapPathC:\ExampleSites\TestMapPath
RootLevelPage.aspxRootLevelPage.aspx "/DownOneLevel/DownLevelPage.aspx""/DownOneLevel/DownLevelPage.aspx" C:\ExampleSites\TestMapPath\DownOneLevel\DownLevelPage.aspxC:\ExampleSites\TestMapPath\DownOneLevel\DownLevelPage.aspx
RootLevelPage.aspxRootLevelPage.aspx "/NotRealFolder""/NotRealFolder" C:\ExampleSites\TestMapPath\NotRealFolderC:\ExampleSites\TestMapPath\NotRealFolder
RootLevelPage.aspxRootLevelPage.aspx "../OutsideApplication""../OutsideApplication" HttpException
/DownOneLevel/DownLevelPage.aspx/DownOneLevel/DownLevelPage.aspx null C:\ExampleSites\TestMapPath\DownOneLevelC:\ExampleSites\TestMapPath\DownOneLevel
/DownOneLevel/DownLevelPage.aspx/DownOneLevel/DownLevelPage.aspx "../RootLevelPage.aspx""../RootLevelPage.aspx" C:\ExampleSites\TestMapPath\RootLevelPage.aspxC:\ExampleSites\TestMapPath\RootLevelPage.aspx