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 的實體檔案路徑。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


如果pathnull,則MapPath方法會傳回包含路徑目前要求之目錄的完整實體路徑。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.

位於的 Web 應用程式C:\ExampleSites\TestMapPath會傳回下列結果: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