The New Intrinsics





The New Intrinsics

This chapter ends with a more detailed look at the new classes provided by ASP.NET to replace the old intrinsic objects in ASP. These include the HttpRequest, HttpResponse, and HttpServerUtility classes. While a lot of the contents of these classes will be familiar to developers who have worked with the traditional ASP instrinsics, there are also several new properties and methods with which ASP.NET developers should become acquainted. Listing 1-17 shows the HttpRequest class, which takes on the responsibilities of the old Request intrinsic in ASP.

-17 HttpRequest Class
NonInheritable Public Class HttpRequest

  Public ReadOnly Property AcceptTypes As String ()
  Public ReadOnly Property ApplicationPath As String
  Public Property Browser As HttpBrowserCapabilities
  Public ReadOnly Property ClientCertificate _
                  As HttpClientCertificate
  Public ReadOnly Property ContentEncoding As Encoding
  Public ReadOnly Property ContentLength As Integer
  Public ReadOnly Property ContentType As String
  Public ReadOnly Property Cookies As HttpCookieCollection
  Public ReadOnly Property CurrentExecutionFilePath _
                  As String
  Public ReadOnly Property FilePath As String
  Public ReadOnly Property Files    As HttpFileCollection
  Public          Property Filter   As Stream
  Public ReadOnly Property Form     As NameValueCollection
  Public ReadOnly Property Headers  As NameValueCollection
  Public ReadOnly Property HttpMethod As String
  Public ReadOnly Property InputStream As Stream
  Public ReadOnly Property IsAuthenticated As Boolean
  Public ReadOnly Property IsSecureConnection As Boolean
  Public ReadOnly Property Params As NameValueCollection
  Public ReadOnly Property Path   As String
  Public ReadOnly Property PathInfo As String
  Public ReadOnly Property PhysicalApplicationPath _
                           As String
  Public ReadOnly Property PhysicalPath As String
  Public ReadOnly Property QueryString _
                           As NameValueCollection
  Public ReadOnly Property RawUrl As String
  Public          Property RequestType As String
  Public ReadOnly Property ServerVariables _
                           As NameValueCollection
  Public ReadOnly Property TotalBytes  As Integer
  Public ReadOnly Property Url         As Uri
  Public ReadOnly Property UrlReferrer As Uri
  Public ReadOnly Property UserAgent   As String
  Public ReadOnly Property UserHostAddress As String
  Public ReadOnly Property UserHostName    As String
  Public ReadOnly Property UserLanguages   As String ()
    ' Methods
  Public Function BinaryRead(count As Integer) As Byte()
  Public Function MapImageCoordinates(name As String) _
                  As Integer()
  Public Function MapPath(path As String) As String
  Public Sub      SaveAs(filename As String, _
                         includeHdrs As Boolean)
End Class

Many properties of the HttpRequest class are type-safe accessors to underlying server variables. Although you can still access all these properties using the ServerVariables collection, as you can in traditional ASP, it is usually more convenient and type-safe to access the information using the provided property. For example, the Url property is an instance of the Uri class that provides an interface to interact with any URI. Several new properties also are available in the HttpRequest class, such as the Browser property, which provides a collection of information about the capabilities of the current client in the form of the HttpBrowserCapabilities class, the full features of which are described in Chapter 8. Another new addition is the Filter property exposed by both the HttpRequest and HttpResponse classes. With the Filter property, you can define your own custom Stream-derived class through which the entire contents of the request (or response) will pass, giving you the opportunity to change the request or response stream at a very low level. Chapter 4 further discusses request and response filters.

Similarly, the HttpResponse class is used to represent the state of the response during the processing of a request. Listing 1-18 shows the main properties and methods available in the HttpResponse class.

-18 HttpResponse Class
public sealed class HttpResponse
NonInheritable Public Class HttpResponse
  Public          Property Buffer As Boolean
  Public          Property BufferOutput As Boolean
  Public ReadOnly Property Cache As HttpCachePolicy
  Public          Property CacheControl As String
  Public          Property Charset As String
  Public          Property ContentEncoding As Encoding
  Public          Property ContentType As String
  Public ReadOnly Property Cookies As HttpCookieCollection
  Public          Property Expires As Integer
  Public          Property ExpiresAbsolute   As DateTime
  Public          Property Filter            As Stream
  Public ReadOnly Property IsClientConnected As Boolean
  Public ReadOnly Property Output            As TextWriter
  Public ReadOnly Property OutputStream      As Stream
  Public          Property Status            As String
  Public          Property StatusCode        As Integer
  Public          Property StatusDescription As String
  Public          Property SupressContent    As Boolean
    ' Methods
  Public Sub AddHeader(name As String, value As String)
  Public Sub AppendCookie(cookie As HttpCookie)
  Public Sub AppendHeader(name As String, value As String)
  Public Sub AppendToLog(value As String)
  Public Sub BinaryWrite(data As Byte())
  Public Sub Clear()
  Public Sub ClearContent()
  Public Sub ClearHeaders()
  Public Sub Close()
  Public Sub End()
  Public Sub Flush()
  Public Sub Pics(value As String)
  Public Sub Redirect(url As String)
  Public Sub SetCookie(cookie As HttpCookie)
  Public Sub Write(value As String)
  Public Sub WriteFile(fileName As String)
End Class

Similar to the request class, the HttpResponse class provides all the familiar methods and properties that are exposed by the traditional ASP Response object. It also provides new features that fill some holes that existed in the old ASP model. For example, the Cache property provides access to an application-wide data cache, the details of which are discussed in Chapter 9.

Finally, the server utility functions are still accessible through the Server property of a page, but the functionality is now encapsulated in the HttpServerUtility class, shown in Listing 1-19.

-19 HttpServerUtility Class
NonInheritable Public Class HttpServerUtility

  Public ReadOnly Property MachineName   As String
  Public          Property ScriptTimeout As Integer

    ' Methods
  Public Sub      ClearError()
  Public Function CreateObject(obj As String) As Object
  Public Function CreateObject(objType As Type) As Object
  Public Function CreateObjectFromClsid(clsid As String) _
                  As Object
  Public Sub      Execute(url As String)
  Public Sub Execute(url As String, output As TextWriter)
  Public Function GetLastError() As Exception
  Public Function HtmlDecode(value As String) As String
  Public Function HtmlEncode(value As String) As String
  Public Function MapPath(path As String) As String
  Public Sub      Transfer(url As String)
  Public Function UrlDecode(url As String) As String
  Public Function UrlEncode(url As String) As String
  Public Function UrlPathEncode(url As String) As String
End Class

Like the other two intrinsic replacements, the HttpServerUtility class provides a mix of familiarity and new features for ASP developers. HtmlEncode and HtmlDecode provide conversions to and from HTML-compatible strings, translating characters that need escaping, such as "<", into HTML escape sequences such as "&lt;", and back again. Similarly, UrlEncode and UrlDecode translate characters that need escaping within a URL, such as "?" or "/".


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows