HttpRequest.MapPath Method (String)
Maps the specified virtual path to a physical path.
Assembly: System.Web (in System.Web.dll)
Parameters
- virtualPath
-
Type:
System.String
The virtual path (absolute or relative) for the current request.
| Exception | Condition |
|---|---|
| HttpException | No HttpContext object is defined for the request. |
Caution |
|---|
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="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>
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.
Imports System 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 'NewNew ' 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 'SetLength Public Overrides Sub Close() strSink.Close() End Sub 'Close Public Overrides Sub Flush() strSink.Flush() End Sub 'Flush 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 'Write End Class 'UpperCaseFilter End Namespace
Available since 1.1
.jpeg?cs-save-lang=1&cs-lang=vb)