1.7 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.

Listing 1-17 HttpRequest Class
 public sealed class HttpRequest {   public string[]                AcceptTypes     {get;}   public string                  ApplicationPath {get;}   public HttpBrowserCapabilities Browser         {get; set;}   public HttpClientCertificate   ClientCertificate {get;}   public Encoding                ContentEncoding {get;}   public int                     ContentLength   {get;}   public string                  ContentType     {get;}   public HttpCookieCollection    Cookies         {get;}   public string        CurrentExecutionFilePath  {get;}   public string                  FilePath        {get;}   public HttpFileCollection      Files           {get;}   public Stream                  Filter          {get; set;}   public NameValueCollection     Form            {get;}   public NameValueCollection     Headers         {get;}   public string                  HttpMethod      {get;}   public Stream                  InputStream     {get;}   public bool                   IsAuthenticated  {get;}   public bool                IsSecureConnection  {get;}   public NameValueCollection    Params           {get;}   public string                 Path             {get;}   public string                 PathInfo         {get;}   public string         PhysicalApplicationPath  {get;}   public string                 PhysicalPath     {get;}   public NameValueCollection    QueryString      {get;}   public string                 RawUrl           {get;}   public string                 RequestType      {get; set;}   public NameValueCollection    ServerVariables  {get;}   public int                    TotalBytes       {get;}   public Uri                    Url              {get;}   public Uri                    UrlReferrer      {get;}   public string                 UserAgent        {get;}   public string                 UserHostAddress  {get;}   public string                 UserHostName     {get;}   public string[]               UserLanguages    {get;}     // Methods   public byte[] BinaryRead(int count);   public int[]  MapImageCoordinates(string name);   public string MapPath(string path);   public void   SaveAs(string filename, bool includeHdrs); } 

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.

Listing 1-18 HttpResponse Class
 public sealed class HttpResponse {   public bool                 Buffer            {get; set;}   public bool                 BufferOutput      {get; set;}   public HttpCachePolicy      Cache             {get;}   public string               CacheControl      {get; set;}   public string               Charset           {get; set;}   public Encoding             ContentEncoding   {get; set;}   public string               ContentType       {get; set;}   public HttpCookieCollection Cookies           {get;}   public int                  Expires           {get; set;}   public DateTime             ExpiresAbsolute   {get; set;}   public Stream               Filter            {get; set;}   public bool                 IsClientConnected {get;}   public TextWriter           Output            {get;}   public Stream               OutputStream      {get;}   public string               Status            {get; set;}   public int                  StatusCode        {get; set;}   public string               StatusDescription {get; set;}   public bool                 SupressContent    {get; set;}     // Methods   public void AddHeader(string name, string value);   public void AppendCookie(HttpCookie cookie);   public void AppendHeader(string name, string value);   public void AppendToLog(string value);   public void BinaryWrite(byte[] data);   public void Clear();   public void ClearContent();   public void ClearHeaders();   public void Close();   public void End();   public void Flush();   public void Pics(string value);   public void Redirect(string url);   public void SetCookie(HttpCookie cookie);   public void Write(string value);   public void WriteFile(string fileName); } 

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.

Listing 1-19 HttpServerUtility Class
 public sealed class HttpServerUtility {   public string MachineName {get;}   public int ScriptTimeout {get; set;}     // Methods   public void ClearError();   public object CreateObject(string obj);   public object CreateObject(Type objType);   public object CreateObjectFromClsid(string clsid);   public void Execute(string url);   public void Execute(string url, TextWriter output);   public Exception GetLastError();   public string HtmlDecode(string value);   public string HtmlEncode(string value);   public string MapPath(string path);   public void Transfer(string url);   public string UrlDecode(string url);   public string UrlEncode(string url);   public string UrlPathEncode(string url); } 

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 " / ".



Essential ASP.NET With Examples in C#
Essential ASP.NET With Examples in C#
ISBN: 0201760401
EAN: 2147483647
Year: 2003
Pages: 94
Authors: Fritz Onion

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net