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
)
public String MapPath (
	String virtualPath
)
public function MapPath (
	virtualPath : String
) : String

Parameters

virtualPath

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

Return Value

The physical path on the server specified by virtualPath.

Exception typeCondition

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"  %>
<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>
  <head>
    <title>HttpResponse.MapPath Example</title>
  </head>
  <body>
    <form runat="server">

      <asp:Label id="Message" 
                 runat="server"/>

    </form>
  </body>
</html>


<%@ Page Language="VJ#" %>
<%@ import Namespace="Samples.AspNet" %>
<script runat="server">

    private void Page_Load(Object sender, EventArgs e)
    {
       
       // Filter the text to be rendered as all uppercase.
       get_Response().set_Filter(new UpperCaseFilterStream(get_Response().
           get_Filter()));
    
    } //Page_Load

</script>
<html>
<head>
</head>
<body>
    <% get_Response().Write("This text will be rendered all uppercase." ); %>
</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);
       
      }
   }
}

package Samples.AspNet;

import System.*;
import System.IO.*;

public class UpperCaseFilterStream extends Stream
{
    // This filter changes all characters passed through it to uppercase.
    private Stream strSink;

    private long lngPosition;

    public UpperCaseFilterStream(Stream sink)
    {
        strSink = sink;
    } //UpperCaseFilterStream

    // The following members of Stream must be overriden.
    /** @property 
     */
    public boolean get_CanRead()
    {
        return true;
    } //get_CanRead

    /** @property 
     */
    public boolean get_CanSeek()
    {
        return true;
    } //get_CanSeek

    /** @property 
     */
    public boolean get_CanWrite()
    {
        return true;
    } //get_CanWrite

    /** @property 
     */
    public long get_Length()
    {
        return 0;
    } //get_Length

    /** @property 
     */
    public long get_Position()
    {
        return lngPosition;
    } //get_Position

    /** @property 
     */
    public void set_Position(long value)
    {
        lngPosition = value;
    } //set_Position

    public long Seek(long offset, System.IO.SeekOrigin direction)
    {
        return strSink.Seek(offset, direction);
    } //Seek

    public void SetLength(long length)
    {
        strSink.SetLength(length);
    } //SetLength

    public void Close()
    {
        strSink.Close();
    } //Close

    public void Flush()
    {
        strSink.Flush();
    } //Flush

    public int Read(ubyte buffer[], int offset, int count)
    {
        return strSink.Read(buffer, offset, count);
    } //Read

    // The Write method actually does the filtering.
    public void Write(ubyte buffer[], int offset, int count)
    {
        ubyte data[] = new ubyte[count];
        Buffer.BlockCopy(buffer, offset, data, 0, count);
        
        for (int i = 0; i < count; i++) {
            // Change lowercase chars to uppercase.
            if (data[i] >= 'a' && data[i] <= 'z') {
                data[i] -= (Convert.ToInt16('a') - Convert.ToInt16('A'));
                data[i] -= (Convert.ToInt16('a') - Convert.ToInt16('A'));
            }
        }
        strSink.Write(data, 0, count);
    } //Write 
} //UpperCaseFilterStream

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft