Although a variety of new debugging features in the Visual Studio .NET IDE can be used in debugging your ASP.NET applications, some old methods and tricks from the days of ASP are still worthwhile.
Previous versions of ASP are built on the foundation of five objects: Response, Request, Server, Application, and Session. The Response object is used to send information from the server down to the client’s browser. The Write method of the Response object can be used to dynamically write content to the client’s browser. This is one of the easiest ways to debug standard ASP pages and is still applicable in the new architecture.
The problem with this approach is that it isn’t very pretty. At the end of debugging a long logic process, you will wind up with a pile of if/then statements and Response.Write() calls littered throughout your .asp pages. This output can get lost inside the HTML if it’s not placed properly. A better approach is to create a specific debugging object that outputs important and pertinent information but handles it in a much nicer and cleaner fashion.You will write an object like this later in the chapter so that you can use it very easily in your ASP debugging procedures.
Internet Information Server (IIS) 5.0 included a new method on the Server object, called GetLastError. This method returns an ASPError object that contains almost everything you need to know about the error except how to correct it. Table 2.1 shows what properties are available on the ASPError object.
ASP Code | Error Code from IIS |
---|---|
Number | COM error code |
Source | Source of line that caused error |
Category | Type of error (ASP, script, object) |
File | ASP file where error occurred |
Line | Line number where error occurred |
Column | Column where error occurred |
Description | A text description of the error |
ASPDescription | Detailed description if error was ASP-related |
You will notice that the information returned in the object is the same information that is presented to you in a standard ASP error page. However, with this information at your disposal, you can create a custom error page that is displayed instead of the standard ASP error page.You can set the error to your own custom page by using the IIS Admin tool. If a custom page is selected, then when the server encounters an error, it will perform a Server.Transfer to the error page, maintaining all state information to the new page. This enables you to get an instance of the ASPError object and pull out the pertinent information for display as a debugging guide. Listing 2.3 shows such a page.
<% Dim Err Set Err = Server.GetLastError() %> <HTML> <HEAD> <TITLE>Error</TITLE> </HEAD> <BODY> An error has occurred! <p> <table border> <tr> <td>Description</td> <td><%=Err.Description%></td> </tr> <tr> <td>Number</td> <td><%=Err.Number%></td> </tr> <tr> <td>Category</td> <td><%=Err.Category%></td> </tr> <tr> <td>File</td> <td><%=Err.File%><</td> </tr> <tr> <td>Line</td> <td><%=Err.Line%><;/td> </tr> <tr> <td>Column</td> <td><%=Err.Column%></td> </tr> <td>Source</td> <td><%=Err.Source%></td> </tr> <tr> <td>ASP Description</td> <td><%=Err.ASPDescription%></td> </tr> </table> </BODY> </HTML> <% Set Err = Nothing%> |