Export (0) Print
Expand All

HttpRequest.MapPath Method (String)

Maps the specified virtual path to a physical path.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

public string MapPath(
	string virtualPath
)

Parameters

virtualPath
Type: System.String

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

Return Value

Type: System.String
The physical path on the server specified by virtualPath.

ExceptionCondition
HttpException

No HttpContext object is defined for the request.

Caution noteCaution

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

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);

      }
   }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft