HttpRequest.MapPath 메서드

정의

요청된 URL의 가상 경로를 현재 요청에 대한 서버의 실제 경로에 매핑합니다.

오버로드

MapPath(String)

지정된 가상 경로를 실제 경로에 매핑합니다.

MapPath(String, String, Boolean)

지정된 가상 경로를 실제 경로에 매핑합니다.

MapPath(String)

지정된 가상 경로를 실제 경로에 매핑합니다.

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

virtualPath에서 지정한 서버 상의 실제 경로입니다.

예외

요청에 대해 정의된 HttpContext 개체가 없습니다.

예제

다음 코드 예제에서는 가상 경로를 서버의 MapPath 정규화된 물리적 경로로 변환하는 방법을 사용합니다. 이 예제에는 두 부분이 있습니다.

  • .aspx 페이지는 경로를 매핑하고, 파일을 읽고, 읽기 작업의 결과를 표시합니다.

  • 전달된 모든 문자를 대문자로 변경하는 클래스 UpperCaseFilterStream입니다.

예제의 첫 번째 부분에서는 메서드를 사용하여 가상 경로를 정규화된 실제 경로로 변환하는 MapPath 방법을 보여 있습니다. 이 실제 경로는 파일의 내용을 가져오는 개체에 전달 StreamReader 됩니다. 그런 다음 메서드 Write 를 호출하여 페이지에 파일의 내용을 표시합니다. 이 Filter 속성은 페이지에 표시되는 텍스트를 모두 대문자로 만드는 응답 스트림에 필터를 연결하는 데 사용됩니다.

<%@ 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 하고 변환하는 클래스를 보여 줍니다. 이 코드를 배치 합니다 App_Code 애플리케이션에 대 한 폴더입니다.


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.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

        ' 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


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


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


        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

    End Class

End Namespace

설명

주의

이 속성에는 MapPath 호스팅 환경에 대한 중요한 정보가 포함될 수 있습니다. 반환 값은 사용자에게 표시되지 않아야 합니다.

적용 대상

MapPath(String, String, Boolean)

지정된 가상 경로를 실제 경로에 매핑합니다.

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

현재 요청의 가상 경로(절대 또는 상대)입니다.

baseVirtualDir
String

상대 주소 확인에 사용되는 가상 기본 디렉터리 경로입니다.

allowCrossAppMapping
Boolean

virtualPath가 다른 애플리케이션에 해당될 수 있으면 true이고, 그러지 않으면 false입니다.

반환

String

서버 상의 실제 경로입니다.

예외

allowCrossMappingfalse이고 virtualPath가 다른 응용 프로그램에 속합니다.

또는

요청에 대해 정의된 HttpContext 개체가 없습니다.

설명

주의

이 속성에는 MapPath 호스팅 환경에 대한 중요한 정보가 포함될 수 있습니다. 반환 값은 사용자에게 표시되지 않아야 합니다.

적용 대상