Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
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


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.


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

        // Read the contents of the file using a StreamReader.
        using (StreamReader sr = new StreamReader(fullpath))
        while (sr.Peek() >= 0)
        Message.Text = "Reading the file was successful.";

      catch (Exception ex)
        Message.Text = "The process failed.";

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

      <asp:Label id="Message" 


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)

      public override void Close()

      public override void 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.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
© 2015 Microsoft