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 使用して、仮想パスをサーバー上の完全修飾物理パスに変換します。 この例には、次の 2 つの部分があります。

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

この例の 2 番目の部分は、ストリーム内のすべての文字を 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

サーバー上の物理パス。

例外

allowCrossMappingfalsevirtualPath は別のアプリケーションに属します。

  • または -

要求に対して HttpContext オブジェクトが定義されていません。

注釈

注意事項

このプロパティには MapPath 、ホスティング環境に関する機密情報が含まれている可能性があります。 戻り値はユーザーに表示しないでください。

適用対象