HttpRequest.MapPath Method

Definition

Maps the virtual path in the requested URL to a physical path on the server for the current request.

Overloads

MapPath(String)

Maps the specified virtual path to a physical path.

MapPath(String, String, Boolean)

Maps the specified virtual path to a physical path.

MapPath(String)

Maps the specified virtual path to a physical path.

public string MapPath (string virtualPath);

Parameters

virtualPath
String

The virtual path (absolute or relative) for the current request.

Returns

The physical path on the server specified by virtualPath.

Exceptions

No HttpContext object is defined for the request.

Examples

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:

  • An .aspx page maps the path, reads the file, and displays results of the read operation.

  • A class, UpperCaseFilterStream, that changes all characters passed through it to uppercase.

The first part of the example shows how to convert a virtual path to a fully qualified physical path using the MapPath method. This physical path is then passed to a StreamReader object, which obtains the contents of the file. The Write method is then called to display the content of the file on the page. 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>

The second part of the example shows a class that inherits from Stream and converts all characters in a stream to uppercase. 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);
      }
   }
}

Remarks

注意

The MapPath property potentially contains sensitive information about the hosting environment. The return value should not be displayed to users.

Applies to

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

MapPath(String, String, Boolean)

Maps the specified virtual path to a physical path.

public string MapPath (string virtualPath, string baseVirtualDir, bool allowCrossAppMapping);

Parameters

virtualPath
String

The virtual path (absolute or relative) for the current request.

baseVirtualDir
String

The virtual base directory path used for relative resolution.

allowCrossAppMapping
Boolean

true to indicate that virtualPath may belong to another application; otherwise, false.

Returns

The physical path on the server.

Exceptions

allowCrossMapping is false and virtualPath belongs to another application.

-or-

No HttpContext object is defined for the request.

Remarks

注意

The MapPath property potentially contains sensitive information about the hosting environment. The return value should not be displayed to users.

Applies to

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1