DekGenius.com
[ Team LiB ] Previous Section Next Section

16.1 Comments/Troubleshooting

In ASP, the Request object provided relatively few properties and methods (one each, in fact), supplying most of the information from requests through its collections: ClientCertificate, Cookies, Form, QueryString, and in particular, the ServerVariables collection. With the exception of ClientCertificate (which now returns an instance of the HttpClientCertificate class representing the client's security certificate settings), all of these collections also exist in ASP.NET. A big difference is that the HttpRequest class exposes a substantial number of new properties (many of which are derived from information that was previously available only through the ServerVariables collection), as well as several new methods.

As was the case with ASP, you can request particular GET or POST values (or ServerVariable or Cookie values, for that matter) by passing the key for the value to the Request object (the current instance of the HttpRequest class):

Message.Text = Request("myKey")

If the key "myKey" exists in any of the collections that the HttpRequest class exposed, the previous code will return it.

Although accessing values as shown in the previous example may seem easy, there are two very good reasons not to use this method.

First, accessing values without specifying the collection in which the value should be found requires ASP.NET to search through each collection until it finds the key (if it finds it). While ASP.NET generally performs significantly faster than ASP, there is still no reason to suffer the unnecessary overhead of this method of accessing values.

Second, using the method shown previously makes your code more difficult to understand, debug, and maintain. Someone attempting to understand how your page operates would not be able to figure out from this code whether the page was expected to be accessed via a GET request or a POST request. Explicitly specifying the desired collection clarifies your intent and makes it easier to track down a problem if your code doesn't work.

In this chapter, we'll use the following code listing as the basis for most examples in the chapter. Unless otherwise noted, each example will consist of only the Page_Load event handler for that particular example. Any displayed output messages or return values will be shown as the Text property of the ASP.NET Label control named Message or displayed by calling Response.Write:

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load( )
            'Example code will go here
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>
    [ Team LiB ] Previous Section Next Section