Main Page

  Previous Section Next Section

14.1 Comments/Troubleshooting

Many HttpContext class properties are topics in their own right. For instance, the Application, Response, Request, and Session properties are accessible here, but covered in detail elsewhere in this book. A couple of methods within HttpContext, however, might require further explanation.

GetConfig sounds like it might be a way to get the appSettings configuration information mentioned in the previous chapter. While it is possible to use GetConfig("appSettings") for that purpose, it requires some casting. GetConfig returns an instance of System.Configuration.ReadOnlyNameValueCollection, which is a private class. To actually use the returned value, you need to cast it to a System.Collections.Specialized.NameValueCollection. Instead of doing this, using ConfigurationSettings.AppSettings is safer and easier. Why is GetConfig there, then? In addition to the appSettings section, a developer can place custom sections within the configuration files, which is what GetConfig is designed for.

The other somewhat unusual (although much more useful) method in HttpContext is RewritePath. The MSDN documentation on this method is, shall we say, sparse. The real use for this method is to silently redirect the user to a different URL.

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

<%@ Page Language="vb" %>
      <script runat="server">
         Sub Page_Load( )
            'Example code will go here
         End Sub
   <asp:label id="Message" runat="server"/>

Some examples will show Application handlers within global.asax rather than the Page_Load method of a page.

      Previous Section Next Section