HttpRequest.MapPath Method (String)
Assembly: System.Web (in system.web.dll)
| Exception type | Condition |
|---|---|
| No HttpContext object is defined for the request. |
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="VJ#" %>
<%@ import Namespace="Samples.AspNet" %>
<!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.
get_Response().set_Filter(new UpperCaseFilterStream(get_Response().
get_Filter()));
} //Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ASP.NET Example</title>
</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.
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
Caution: