HomeASP.NET Advanced Testing and Debugging of an ASP.N...
Advanced Testing and Debugging of an ASP.NET 2.0 Application
In this article, you will learn how to create a website that runs under IIS for testing purposes. You will also learn how to create and test a file-system website with the ASP.NET Development Server. The third of three parts, this article is excerpted from chapter four of the book Murach's ASP.NET 2.0 Web Programming with VB 2005, written by Joel Murach and Anne Boehm (Murach, 2006; ISBN: 1-890774-32-4).
How to use the Immediate window to work with values
The Immediate window, shown in figure 4-14, is useful for displaying the values of variables or properties that don’t appear in the Code Editor window. To display a value, you simply type a question mark followed by the name of the variable or property. The first line of code in this figure, for example, displays the Text property of the item selected from the Products drop-down list. You can see the result in the second line of this window.
The Immediate window is also useful for executing C# statements. For example, you can execute an assignment statement to change the value of a variable or property. After I displayed the Text property of the Quantity text box, for example, I assigned a value of 1 to this property. Similarly, you can execute a user-defined method. This can be useful for testing the result of a method with different arguments. If you execute a method that returns a value, you can also preface the method name with a question mark to display the value it returns.
When you enter commands in the Immediate window, they’re executed in the same context (or scope) as the application that’s running. That means that you can’t display the value of a variable that’s out of scope. If you try to do that, the debugger displays an error message.
The commands that you enter into the Immediate window remain there until you exit from Visual Studio or explicitly delete them using the Clear All command in the shortcut menu for the window. That way, you can use standard Windows techniques to edit and reuse the same commands from one execution of an application to another without having to reenter them.
To execute a command that you’ve already entered in the Immediate window, just use the Up and Down arrow keys to scroll through the commands. As you scroll, the commands are displayed at the bottom of the window. Then, you can change a command if necessary and press Enter to execute it.
The Immediate window
Figure 4-14. How to use the Immediate window to work with values
Description
You can use the Immediate window to display and assign values from a program during execution. To display this window, click on the Immediate Window tab or use the Debug->Windows->Immediate command.
To display a value in the Immediate window, enter a question mark followed by the expression whose value you want to display. Then, press the Enter key.
To assign a different value to a variable, property, or object, enter an assignment statement in the Immediate window. Then, press the Enter key.
To execute a user-defined method from the Immediate window, enter its name and any arguments it requires. Then, press the Enter key. If you want to display the value that’s returned by a method, precede the method call with a question mark.
To reissue a command, use the Up and Down arrow keys to scroll through the commands until you find the one you want. Then, modify the command if necessary and press the Enter key to execute it.
To remove all commands and output from the Immediate window, use the Clear All command in the shortcut menu for the window.
The Trace feature is an ASP.NET feature that displays some useful information that you can’t get by using the debugger. Because the debugger works so well, you probably won’t need to use the Trace feature very much, but you should at least be aware of the information that it can provide.
How to enable the Trace feature
To use the Trace feature, you must first enable tracing. To do that, you add a Trace attribute to the Page directive for the page, and you assign a value of True to this attribute. Then, when you run the page, trace information will be displayed at the end of the page output, as shown in figure 4-15.
When you enable the Trace feature, it is enabled only for the current page, which is usually what you want. To enable tracing for another page, you must modify the Page directive for that page too. Once this feature has been enabled for a page, ASP.NET adds trace output to the page whenever the page is requested.
How to interpret Trace output
In figure 4-15, you can see the start of the output for the Cart page after the user added an item to the shopping cart. After the request details, the trace information provides a list of trace messages that are generated as the application executes. Here, ASP.NET automatically adds Begin and End messages when major page events such as PreInit, Init, and InitComplete occur. If you scroll down to see all of these trace messages, you can see the variety of events that are raised during the life cycle of a page.
After the trace messages, you’ll find information about the controls used by the page, the items in the session state object, the cookies that were included with the HTTP request, the HTTP request headers, and the server variables. In this figure, for example, you can see the session state and cookies data for the Cart page of the Shopping Cart application. In this case, an item named Cart has been added to the session state object. And a cookie named ASP.NET_SessionId is used to keep track of the user’s session ID so the user’s session state object can be retrieved.
The beginning of the trace output for the Cart page
Figure 4-15. How to enable the Trace feature and interpret Trace output
The session and cookies information for the Cart page
A Page directive that enables tracing for the Cart page
The ASP.NET Trace feature traces the execution of a page and displays trace information and other information at the bottom of that page.
To activate the trace feature for a page, you add a Trace attribute to the Page directive at the top of the aspx file for the page and set its value to True as shown above.
The trace information is divided into several tables that provide specific types of trace information. For example, the Trace Information table provides information about how the page request was processed, and the Session State table provides information about the items currently stored in session state.
In some cases, you may want to add your own messages to the trace information that’s generated by the Trace feature. This can help you track the sequence in which the methods of a form are executed or the changes in the data as the methods are executed. Although you can also do this type of tracking by stepping through the methods of a form with the debugger, the trace information gives you a static listing of your messages.
Note, however, that you can also create this type of listing using tracepoints as described earlier in this chapter. The advantage to using tracepoints is that you can generate trace information without adding code to your application. In addition, this output is generated only when you run an application with debugging. In contrast, you have to add program code to add custom trace messages, and the trace output is generated whenever the Trace feature is enabled. Because of that, you may not want to create custom trace messages. But I’ve included it here in case you do.
To add messages to the trace information, you use the Write or Warn method of the TraceContext object. This is summarized in figure 4-16. The only difference between these two methods is that messages created with the Warn method appear in red. Notice that to refer to the TraceContext object, you use the Trace property of the page.
When you code a Write or Warn method, you can include both a category and a message or just a message. If you include just a message, the category column in the trace output is left blank. If you include a category, however, it appears as shown in this figure. In most cases, you’ll include a category because it makes it easy to see the sequence in which the methods were executed.
If you want to determine whether tracing is enabled before executing a Write or Warn method, you can use the IsEnabled property of the TraceContext object as shown in the example in this figure. Normally, though, you won’t check the IsEnabled property because trace statements are executed only if tracing is enabled.
Common members of the TraceContext class
Property
Description
IsEnabled
True if tracing is enabled for the page.
Method
Description
Write(message)
Writes a message to the trace output.
Write(category, message)
Writes a message to the trace output with the specified category.
Warn(message)
Writes a message in red type to the trace output.
Warn(category, message)
Writes a message in red type to the trace output with the specified category.
Code that writes a custom trace message
if (Trace.IsEnabled) Trace.Write("Page_Load", "Binding products drop-down list.");
A portion of a trace that includes a custom message
Figure 4-16. How to create custom trace messages
Description
You can use the TraceContext object to write your own messages to the trace output. The TraceContext object is available through the Trace property of a page.
Use the Write method to write a basic text message. Use the Warn method to write a message in red type.
Trace messages are written only if tracing is enabled for the page. To determine whether tracing is enabled, you use the IsEnabled property of the TraceContext object.
Another way to display information as a program executes is to write it directly to the HTTP output stream. To do that, you use the Write method of the HTTP Response object as shown in figure 4-17. When you use this technique, you’ll want to be sure to remove any statements you’ve added when you finish testing your application.
At the top of this figure, you can see a Cart page that includes output that indicates the number of items that are currently in the shopping cart. To generate this output, I added a Response.Write method to the Page_Load event handler of the page. As you can see, this event handler uses the Count property of the Cart object to determine the number of items that are in the cart.
Notice that the text you include on the Write method can include HTML tags. For example, the Write method shown here includes a <br /> tag so that the item count is followed by a blank line. Also note that the output you write to the output stream is always added to the beginning of the stream. Because of that, it always appears at the top of the browser window.
The Cart page with output generated by Response.Write
Figure 4-17. How to write information directly to the HTTP output stream
A Page_Load event handler that writes to the HTTP output stream
The Write method of the HttpResponse object provides a convenient way to write data directly to the HTTP output stream. The output can include any valid HTML.
To access the HttpResponse object from the code-behind file for a web page, you use the Response property of the page. To access this object from a class that doesn’t inherit the Page class, you use the Response property of the HttpContext object for the current request. To access this object, you use the Current property of the HttpContext class.
The HTML output you add to the HTTP output stream using the Write method of the HttpResponse object appears at the beginning of the output stream. As a result, the output from the Write method always appears at the top of the page in the browser window.
Perspective
As you can now appreciate, Visual Studio provides a powerful set of tools for debugging ASP.NET applications. For simple applications, you can usually get the debugging done just by using breakpoints, data tips, and the Autos window. You may also need to step through critical portions of code from time to time. For complex applications, though, you may discover the need for some of the other features that are presented in this chapter. With tools like these, a difficult debugging job becomes manageable.