HttpRequest.MapPath HttpRequest.MapPath HttpRequest.MapPath HttpRequest.MapPath Method

定義

將用戶端目前要求之 URL 的虛擬路徑對應至伺服器上的實體路徑。Maps the virtual path in the requested URL to a physical path on the server for the current request.

多載

MapPath(String) MapPath(String) MapPath(String) MapPath(String)

將指定的虛擬路徑對應至實體路徑。Maps the specified virtual path to a physical path.

MapPath(String, String, Boolean) MapPath(String, String, Boolean) MapPath(String, String, Boolean) MapPath(String, String, Boolean)

將指定的虛擬路徑對應至實體路徑。Maps the specified virtual path to a physical path.

MapPath(String) MapPath(String) MapPath(String) MapPath(String)

將指定的虛擬路徑對應至實體路徑。Maps the specified virtual path to a physical path.

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

參數

virtualPath
String String String String

目前要求的虛擬路徑 (絕對或相對的)。The virtual path (absolute or relative) for the current request.

傳回

virtualPath 所指定伺服器上的實際路徑。The physical path on the server specified by virtualPath.

例外狀況

要求未定義任何 HttpContext 物件。No HttpContext object is defined for the request.

範例

下列程式碼範例使用MapPath方法,以將虛擬路徑轉換成在伺服器上的完整實體路徑。The following code example uses the MapPath method to convert a virtual path to a fully qualified physical path on the server. 此範例中有兩個部分:This example has two parts:

  • .Aspx 頁面的路徑對應、 讀取檔案,並會顯示讀取作業的結果。An .aspx page maps the path, reads the file, and displays results of the read operation.

  • 一種類別, UpperCaseFilterStream,變更為大寫通過的所有字元。A class, UpperCaseFilterStream, that changes all characters passed through it to uppercase.

此範例的第一個部分示範如何將虛擬路徑轉換成完整的實體路徑,使用MapPath方法。The first part of the example shows how to convert a virtual path to a fully qualified physical path using the MapPath method. 此實體的路徑會傳遞至StreamReader物件,它會取得檔案的內容。This physical path is then passed to a StreamReader object, which obtains the contents of the file. Write方法接著會呼叫顯示在頁面上的檔案內容。The Write method is then called to display the content of the file on the page. Filter屬性用來將篩選附加至回應資料流,讓 [全部大寫] 頁面上所顯示的文字。The Filter property is used to attach a filter to the response stream that makes the text displayed to the page all uppercase.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ import Namespace="Samples.AspNet.CS.Controls"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
    {

      // Filter the text to be rendered as all uppercase.
      Response.Filter = new UpperCaseFilterStream(Response.Filter);

      // Convert a virtual path to a fully qualified physical path.
      string fullpath = Request.MapPath("~\\TestFile.txt");

      try
      {
        // Read the contents of the file using a StreamReader.
        using (StreamReader sr = new StreamReader(fullpath))
        while (sr.Peek() >= 0)
        {
          Response.Write((char)sr.Read());
        }
        Message.Text = "Reading the file was successful.";
        
      }
      catch (Exception ex)
      {
        Message.Text = "The process failed.";
      }    
     }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>HttpResponse.MapPath Example</title>
  </head>
  <body>
    <form id="form1" runat="server">

      <asp:Label id="Message" 
                 runat="server"/>

    </form>
  </body>
</html>
<%@ Page Language="VB" Debug="true"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Samples.AspNet.VB.Controls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
     
  Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      
    ' Filter the text to be rendered as all uppercase.
    Response.Filter = New UpperCaseFilterStream(Response.Filter)
    
    ' Convert a virtual path to a fully qualified physical path.
    Dim fullpath As String = Request.MapPath("~\\TestFile.txt")
    
    Try
      
      Dim sr As StreamReader = New StreamReader(fullpath)
      
      Do While sr.Peek() >= 0
        Response.Write(Convert.ToChar(sr.Read()))
      Loop
      sr.Close()
      Message.Text = "Reading the file was successful."
      
    Catch ex As Exception
      
      Message.Text = "The process failed."

    End Try

    
  End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>HttpResponse.MapPath Example</title>
  </head>
  <body>
    <form id="Form1" runat="server">

      <asp:Label id="Message" 
                 runat="server"/>

    </form>
  </body>
</html>

此範例的第二部分顯示的類別,繼承自Stream並將資料流中的所有字元都轉換成大寫。The second part of the example shows a class that inherits from Stream and converts all characters in a stream to uppercase. 將此程式碼放入App_Code應用程式資料夾。Put this code in the App_Code folder for your application.


using System;
using System.IO;
using System.Text;

namespace Samples.AspNet.CS.Controls
{

   public class UpperCaseFilterStream : Stream
   // This filter changes all characters passed through it to uppercase.
   {
      private Stream strSink;
      private long lngPosition;

      public UpperCaseFilterStream(Stream sink)
      {
          strSink = sink;
      }

      // The following members of Stream must be overriden.
      public override bool CanRead
      {
         get { return true; }
      }

      public override bool CanSeek
      {
         get { return true; }
      }

      public override bool CanWrite
      {
         get { return true; }
      }

      public override long Length
      {
         get { return 0; }
      }

      public override long Position
      {
         get { return lngPosition; }
         set { lngPosition = value; }
      }

      public override long Seek(long offset, System.IO.SeekOrigin direction)
      {
         return strSink.Seek(offset, direction);
      }

      public override void SetLength(long length)
      {
         strSink.SetLength(length);
      }

      public override void Close()
      {
         strSink.Close();
      }

      public override void Flush()
      {
         strSink.Flush();
      }

      public override int Read(byte[] buffer, int offset, int count)
      {
         return strSink.Read(buffer, offset, count);
      }

      // The Write method actually does the filtering.
      public override void Write(byte[] buffer, int offset, int count)
      {
		 byte[] data = new byte[count];
	     Buffer.BlockCopy(buffer, offset, data, 0, count);
		 string inputstring = Encoding.ASCII.GetString(data).ToUpper();
		 data = Encoding.ASCII.GetBytes(inputstring);
	     strSink.Write(data, 0, count);
       
      }
   }
}
Imports System
Imports System.IO
Imports System.Text


Namespace Samples.AspNet.VB.Controls

    Public Class UpperCaseFilterStream
        Inherits Stream

        ' This filter changes all characters passed through it to uppercase.
        Private strSink As Stream
        Private lngPosition As Long


        Public Sub New(ByVal sink As Stream)
            strSink = sink
        End Sub 'New

        ' The following members of Stream must be overriden.  
        Public Overrides ReadOnly Property CanRead() As Boolean
            Get
                Return True
            End Get
        End Property

        Public Overrides ReadOnly Property CanSeek() As Boolean
            Get
                Return True
            End Get
        End Property

        Public Overrides ReadOnly Property CanWrite() As Boolean
            Get
                Return True
            End Get
        End Property

        Public Overrides ReadOnly Property Length() As Long
            Get
                Return 0
            End Get
        End Property

        Public Overrides Property Position() As Long
            Get
                Return lngPosition
            End Get
            Set(ByVal value As Long)
                lngPosition = Value
            End Set
        End Property

        Public Overrides Function Seek( _
        ByVal offset As Long, ByVal direction As System.IO.SeekOrigin) As Long
            Return strSink.Seek(offset, direction)
        End Function 'Seek


        Public Overrides Sub SetLength(ByVal length As Long)
            strSink.SetLength(length)
        End Sub 'SetLength


        Public Overrides Sub Close()
            strSink.Close()
        End Sub 'Close


        Public Overrides Sub Flush()
            strSink.Flush()
        End Sub 'Flush


        Public Overrides Function Read( _
        ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
            Return strSink.Read(buffer, offset, count)
        End Function 'Read


        ' The Write method actually does the filtering.
        Public Overrides Sub Write( _
        ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)

            Dim data(count) As Byte
            System.Buffer.BlockCopy(buffer, offset, data, 0, count)
            Dim inputstring As String = Encoding.ASCII.GetString(data).ToUpper()
            data = Encoding.ASCII.GetBytes(InputString)
            strSink.Write(data, 0, count)

        End Sub 'Write 

    End Class 'UpperCaseFilter

End Namespace

備註

警告

MapPath屬性可能包含裝載環境的機密資訊。The MapPath property potentially contains sensitive information about the hosting environment. 傳回的值不應該向使用者顯示。The return value should not be displayed to users.

MapPath(String, String, Boolean) MapPath(String, String, Boolean) MapPath(String, String, Boolean) MapPath(String, String, Boolean)

將指定的虛擬路徑對應至實體路徑。Maps the specified virtual path to a physical path.

public:
 System::String ^ MapPath(System::String ^ virtualPath, System::String ^ baseVirtualDir, bool allowCrossAppMapping);
public string MapPath (string virtualPath, string baseVirtualDir, bool allowCrossAppMapping);
member this.MapPath : string * string * bool -> string
Public Function MapPath (virtualPath As String, baseVirtualDir As String, allowCrossAppMapping As Boolean) As String

參數

virtualPath
String String String String

目前要求的虛擬路徑 (絕對或相對的)。The virtual path (absolute or relative) for the current request.

baseVirtualDir
String String String String

用於相對解析的虛擬基底目錄路徑。The virtual base directory path used for relative resolution.

allowCrossAppMapping
Boolean Boolean Boolean Boolean

若為 true,表示 virtualPath 可能屬於另一個應用程式,否則為 falsetrue to indicate that virtualPath may belong to another application; otherwise, false.

傳回

伺服器的實體路徑。The physical path on the server.

例外狀況

allowCrossMappingfalse,並且 virtualPath 屬於另一個應用程式。allowCrossMapping is false and virtualPath belongs to another application.

-或--or- 要求未定義任何 HttpContext 物件。No HttpContext object is defined for the request.

備註

警告

MapPath屬性可能包含裝載環境的機密資訊。The MapPath property potentially contains sensitive information about the hosting environment. 傳回的值不應該向使用者顯示。The return value should not be displayed to users.

適用於